Class References
From The Oxygene Language Wiki
This is a Language topic
Feel free to add your notes to this topic below.
Class references, or meta classes, are classes that describe a class. Class references are supported, as shown by the following example syntax:
type MyClass1 = public class public constructor; virtual; class method TestIt; virtual; end; MyClass2 = public class(Class1) public constructor; override; class method TestIt; override; end; MyClass = class of MyClass1;
Support includes:
- class virtual methods (class method DoSomething; virtual;).
- virtual constructors (constructor; virtual;).
- class types (type x = class of y;).
- using the "new" operator on class type variables to invoke virtual (or non-virtual) constructors.
The ClassRef sample shipped with the product illustrates this feature. You might find putting Reflector to work on the ClassRef sample to be helpful.
Accessing Class References
You can obtain the correct class reference from the instance methods of the class itself via GetMetaClass. This is a public method without parameters and it returns the result type of highest ancestor that has a class reference, or else assigning a type to a class reference type will work, too:
type
MyClass = class
class procedure DoSomething; virtual;
end;
MyClass2 = class(MyClass)
class procedure DoSomething; override;
end;
MyClassRef = class of MyClass;
...
var myclass: MyClass;
myclass := new MyClass2;
var ref: MyClassRef;
ref := myclass.GetMetaClass(); // returns the meta class for MyClass2
..
ref := MyClass2;
Notes
Delphi Prism implements class references in a way that is compatible with other .NET languages: you'll be able to call all code from other languages (although the syntax will not be as pretty, since C# and other languages don't understand the concept of meta classes).
See Also
Area: Oxygene Language
Compiler version: Oxygene 5
Language Glossary — Keywords — Types — FAQ — How To