JCL中由接口获得对象的方法
2007-05-15 21:43
288 查看
我原本并不想单独发以下JCL的方法,可是想到很多人可能还是不知道的。另外,从我博客的回复上看,很多人也愿意去了解一下,特此贴出JCL中的代码。
注释:这段代码出自JCL代码中JclSysUtils单元中。
提示:读这段代码,要明白一个道理,在代码执行过程中,接口和对象指针的偏移是硬编码的在汇编中的。转换的过程,就是解析这段汇编的过程。
//=== Interface information ================================================== function GetImplementorOfInterface(const I: IInterface): TObject; { TODO -cDOC : Original code by Hallvard Vassbotn } { TODO -cTesting : Check the implemetation for any further version of compiler } const AddByte = $04244483; // opcode for ADD DWORD PTR [ESP+4], Shortint AddLong = $04244481; // opcode for ADD DWORD PTR [ESP+4], Longint type PAdjustSelfThunk = ^TAdjustSelfThunk; TAdjustSelfThunk = packed record case AddInstruction: Longint of AddByte: (AdjustmentByte: ShortInt); AddLong: (AdjustmentLong: Longint); end; PInterfaceMT = ^TInterfaceMT; TInterfaceMT = packed record QueryInterfaceThunk: PAdjustSelfThunk; end; TInterfaceRef = ^PInterfaceMT; var QueryInterfaceThunk: PAdjustSelfThunk; begin try Result := Pointer(I); if Assigned(Result) then begin QueryInterfaceThunk := TInterfaceRef(I)^.QueryInterfaceThunk; case QueryInterfaceThunk.AddInstruction of AddByte: Inc(PChar(Result), QueryInterfaceThunk.AdjustmentByte); AddLong: Inc(PChar(Result), QueryInterfaceThunk.AdjustmentLong); else Result := nil; end; end; except Result := nil; end; end;
注释:这段代码出自JCL代码中JclSysUtils单元中。
提示:读这段代码,要明白一个道理,在代码执行过程中,接口和对象指针的偏移是硬编码的在汇编中的。转换的过程,就是解析这段汇编的过程。
相关文章推荐
- JCL中由接口获得对象的方法
- JCL中由接口获得对象的方法
- JCL中由接口获得对象的方法
- JCL中由接口获得对象的方法
- JSP内置对象:getattribute()方法获得数据
- Spring框架中获得DataSource对象的方法
- Java获得Class对象的方法
- Java中获得一个Class对象的方法
- struts2中几种获得request,session等对象的方法
- java从list中取出对象并获得其属性值的方法
- 获取对象的接口信息(方法/属性/事件)(VB6代码)
- Java 基础数据类型 和 深度克隆对象的2种方法(实现Cloneable接口或者实现对象序列化)
- golang里面检测对象是否实现了接口的方法
- 常用方法(1)------根据图片的url路径获得Bitmap对象
- ext中获得对象、组件的方法比较(Ext.getxxx())
- autofac如何注册静态方法里的接口对象
- java之通过反射,来获得某对象的所有方法(类方法提取器)
- Struts2 的 Preparable, ModelDriven 接口方法里无法获得参数,理解 拦截器与责任链
- JS获得一个对象的所有属性和方法
- 五星-原型模式常使用于以下场景--而JAVA中的任何类只要实现了Cloneable标识接口,就可以使用clone方法来进行对象的拷贝