Class References

From RemObjects Software

Jump to: navigation, search

This is a Oxygene Language Feature 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 will 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, 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

Oxygene 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


Product: RemObjects Oxygene (formerly known as Chrome)
Current version: 3.0 Previous Versions: 'Joyride' (2.0), 'Floorshow' (1.5), 'Adrenochrome' (1.0)

GlossaryKeywordsLanguage FeaturesPlatform FeaturesSamplesArticlesHow ToIssues

Personal tools