type Unit = class procedure .cctor(); proceudre HelloWorld(); ...
implementation
procedure Unit..cctor(); begin Borland.Delphi.System.Unit._AddFinalization( new Borland.Delphi.System._FinalizeHandler( null, HelloWorld.Unit.Finalization));
TObjectHelper = class helper for TObject procedure Free; function ClassType: TClass; class function ClassName: string; class function ClassNameIs(const Name: string): Boolean; class function ClassParent: TClass; class function ClassInfo: TObject; class function InheritsFrom(AClass: TClass): Boolean; class function MethodAddress(const Name: string): TObject; class function SystemType: System.Type; function FieldAddress(const Name: string): TObject; procedure Dispatch(var Message); end; //-----------------------------------------Borland.Delphi.System.pas-- 这样一来,Borland就简洁但并不完美的解决了这个悖论。不过可以预见,这种语法的出现, 必将在将来引发激烈的争论,因为无论如何,这种语法事实上冲击了OO设计思想的纯洁性。 后面我们分析Borland.Delphi.System单元时再详细讨论class helper的使用方法。 在TDemo类中,另一个值得注意的是名为@MetaTDemo的嵌套子类。 在Delphi中,每个类都有一个对应的元类 MetaClass,可以通过class of TMyClass定义 TMyClass的元类类型来访问,也可以从类方法中直接通过Self指针访问。元类在实现上就是在 此类对象所共有的VMT表。 而在Delphi.NET中,类的内存布局不再由Delphi完全控制,不大可能将VMT再绑定到每个对象上。 所以Borland通过一个以"@Meta+类名"作为类名称的嵌套子类来表示此类的元类。如TDemo的元类是 TDemo.@MetaTDemo类,从Borland.Delphi.System._TClass类继承出来。 //-----------------------------------------Borland.Delphi.System.pas-- _TClass = class;
TClass = class of TObject;
_TClass = class protected FInstanceType: System.RuntimeTypeHandle; FClassParent: _TClass; public constructor Create; overload; constructor Create(ATypeHandle: System.RuntimeTypeHandle); overload; constructor Create(AType: System.Type); overload; function ClassParent: TClass; virtual; end;