Delphi.net internal implementation analysis (3.3)
2.3. Object Next Let's see instances of the class, the implementation of the object / / --------------------------------------------------------------------------------------------------------------- -------- Borland.Delphi.System.pas - type TObject = System.Object; 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; VAR message; end; // ----------------------------- ------------ borland.delphi.system.pas - From Borland.Delphi.system's definition we can see, Tobject is actually the alias of System.Object, and the real code It is the patch to TOBJECT through Class Helper is the patch of System.Object. Let's take a closer look at the implementation code of TOBJECTHELPER, understand how Delphi is ported to the CLR. //----------------------------------------- ,land.delphi.system.pas --Procedure TobjectHelper.Free; Begin if (Self <> nil) THEN (Self as idisposable) .dispose; end; // ---------------- ------------------------- borland.delphi.system.pas - Different memory mode with traditional Delphi users, Delphi.net uses CLR The automatic memory management mechanism provided, has a GC garbage collection mechanism automatically reclaims useless memory. However, in order to give users a chance to control external resources such as file handle, network connection, CLR provides a compromise mechanism for iDisposable interfaces. If a class implements the IDisposable interface, it indicates that it has the need to manage the lifecycle of the lifecycle, and can be manually released through the idisposable.dispose method. The interface is defined as followsposable = interface procedure dispose; end; thus Delphi.NET's Free method simply detects whether the current object supports the IDisposable interface, if you support call IDisposable.dispose release resources.
However, the preview version of the interface is not very easy to use: (if you can support the function like a USING statement in the C # in the future, hehe you can see in the implementation of TobjectHelper, etc., such as the class method, such as the Class Function method Said that self is the class of class, so the classparent and ClassInfo methods are directly obtained from their metaccies .//----------------------------------------------------------- . ClassInfo: TOBJECT; Begin Result: = _TCLASS (Self) .finstanceTancetype; End; // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------- borland.delphi.system.pas-- and for ordinary methods, you need to convert it to Token or Type again. ClasStype and FieldadDress are two good examples. Former Use the _getmetaFromHandle function to get the category from the Tykeken of the current object; the latter obtains the object of the specified name field from the Type of the current object (the concept of similar meta-class class). -------- ----------------------------------- borland.delphi.system.pas - function TobjectHelper.ClasStype: Tclass ; begin Result: = _GetMetaFromHandle (System.Type.GetTypeHandle (Self)); end; function TObjectHelper.FieldAddress (const Name: string): TObject; begin Result: = TypeOf (Self) .GetField (Name); end; // ---------------------------------------- borland.delphi.system .pas - The principle of the realization of the remaining method is small, nothing more than a transition between objects, classes, categories, token, and Type to obtain the required information.