Delphi.net internal implementation analysis (3.4)

zhaozj2021-02-16  62

Delphi.net internal implementation analysis (3.4)

From this we can see that Delphi.NET uses a method from the in-nepe Class Helper, in order to finalize the process of migrating from traditional inheritance models and memory models to CLR and FCL tree, the migration process cannot be hardships. Although this solution cannot be perfect, I believe that Borland has made such choices after comprehensively assessing many other means, paying some costs, such as Class Helper, has also achieved a lot of results, source code level compatibility Strong. This mapping model does not work, I think I can only have time to do comments. Finally, let's see how Delphi's IS and AS key are implemented in delphi.net ------------------------- -------------- borland.delphi.system.pas - function _isclass (Obj: Tobject; CLS: Tclass): Boolean; Var T1, T2: System.Type; Begin if not assigned (Obj) THEN RESE: = false else begin t1: = obj.gettype; t2: = system.type.gettypefromhandle (_TClass (CLS) .finstanceType); if t1 = t2 Then Result: = true else result: = t1.issubclassof (t2); end; end; // ----------------------------------------------------------------------------------------------------------------------------------------------------------- -Borland.delphi.system.pas - _ISCLASS function is very simple, and the detection target is validated and directly detected by judging two types of inheritance. //-----------------------------------------System.pas --function _ISCLASS (Child: Tclass): Boolean; Begin Result: = (Child <> nil) and child.inheritsfrom (pent); end; // ---------------------------------------------------------------------------------- --------------------------- System.Pas - Compared to Delphi's IS implementation is simpler, direct use of Tobject.inheritsFrom. Delphi.net is not like Delphi to use TOBJECT.INHERITSFROM to implement the IS keyword because it is relative to the Type.issubclassof method, the Type.isinstanceOfTAM method used by TOBJECTHELPER.INHERITSFROM method is large. The Type.issubclassof method only starts with the income type, and the first level will check if his parent class is yourself.

//----------------------------Type.cs - Public Abstract ABSTRACT Class Type: MemberInfo, Ireflect {Public Virtual Bool Issubclassof (Type C) {TYPE P = this; if (p == c) Return False; While (p! = null) {if (p == c) Return True; p = P.BaseType;} Return False;}} // ------------------------------------------------------------------------------------------------------------------------------------------------------------ ---- Type.cs - Type.isInstanceOfType should consider remmboting, COM, interfaces, and runtime types, etc., etc., etc. //----------------------------------------- ,land.delphi.system.pas --Function_asclass (OBJ: TOBJECT; CLS: Tclass): Tobject; Begin Result: = Obj; if not_isclass (obj, cls) Then Raise System.Formatexception.create ('Invalid Cast'); End; // -------------------------------------- borland.delphi.system.pas - AS operation The implementation of the characters is just a simple assignment, because the CLR is a single structure, so the conversion is always successful, and only the IS operator is detected after the conversion, throwing an abnormal situation. //-----------------------------System.pas --function _asclass (Child: tclass): TOBJECT; {$ ifdef purepascal} Begin Result: = child; if not (childiD); // Loses returnad; // ------ ----------------------------------- System.Pas - You can see that the implementation in Delphi is also very similar. of. The last correlation function is _ClassCreate for implementing the type of creation and constructing.

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

New Post(0)