Delphi.net internal implementation analysis (3.2)

zhaozj2021-02-16  55

Delphi.net internal implementation analysis (3.2)

First let's take a look at the definition and implementation of the yuan class / / ------------------------------------- ---- Borland.Delphi.System.pas - type TObject = System.Object; _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; TClassHelperBase = class (TObject) public FInstance: TObject; end; // - --------------------------------------- borland.delphi.system.pas - Previous We probably analyze the implementation principle of the classification. Each class has a corresponding nested subclass with the @meta prefix plus class name, inherited from the borland.delphi.system._tclass class, similar to Tclasshelperbase, but Finstance is a class level Static member variable. .class / * 0200000D * / auto ansi nested public beforefieldinit @MetaTDemo extends Borland.Delphi.System. @ TClass / * 02000003 * / {.field / * 0400000E * / public static class HelloUnit.TDemo / * 02000005 * // @ MetaTDemo / * 0200000D * / @Instance .method / * 06000027 * / private hidebysig specialname rtspecialname static void .cctor () cil managed .method / * 06000026 * / public hidebysig specialname rtspecialname instance void .ctor () cil managed .method / * 06000008 * / Public instance void Hello () CIL Managed} The static constructor of the Metclass is as follows, the @ metaterdemo..cctor () function in the previous section constructs an instance of a class class into the @Instance static member Private Static TDemo. @MetATDemo..cctor () {TDEMO. @ Instance @ @ instance = new @ metaddemo..ctor ();} The constructor of the mid class loads your own TOKEN to FINSTANCEPE field.

Public tdemo. @ MetaCemo..ctor (): base () {this.finstanceType = token of tdemo;} This token's role in the CLR to the index, to locate a specific entry in MultaData, each Elements such as classes, methods, fields, properties, etc. have their own token. In the BCL, TokeN is used as a RuntimeTypeHandle type, which is used for functions such as Type.gettypeFromHandle, which can be obtained via Type.TypeHandle. For the importance of Token and physical implementation, please participate in the previous article I mentioned in Metadata analysis. The FINSTANCETYPE saved here is actually similar to the index of the VMT pointer in Delphi. //----------------------------------------- ,land.delphi.system.pas --constructor _TClass.Create; begin inherited Create; end; constructor _TClass.Create (ATypeHandle: System.RuntimeTypeHandle); begin inherited Create; FInstanceType: = ATypeHandle; end; constructor _TClass.Create (AType: System.Type); begin Create (Atype.typehandle; end; // ---------------------------------------- -Borland.delphi.system.pas - You can see several form constructor of _TClass, actually surround this token. //----------------------------------------- ,land.delphi.system.pas --function _TClass.ClassParent: TClass; begin if not Assigned (FClassParent) then FClassParent: = _TClass.Create (System.Type.GetTypeFromHandle (FInstanceType) .BaseType.TypeHandle); Result: = FClassParent; end; // --- -------------------------------------- Borland.Delphi.system.pas - _tclass .Fclassparent is the category of the parent class filled when needed. Note the method of converting the TOKEN to CLR here. System.Type.gettypeFromHandle function and the system.type.gettypeHandle function to use later complete the two-way conversion between Type and Token. Therefore, in delphi.net, the category is actually a package for TOKEN of the class. After understanding the implementation of the Metai, let's take a look at how to get its category from an Token.

转载请注明原文地址:https://www.9cbs.com/read-22621.html

New Post(0)