Delphi中接口方法索引如何快速定位?
- 内容介绍
- 文章标签
- 相关推荐
本文共计531个文字,预计阅读时间需要3分钟。
要找到Interface中定义的过程/函数的引用,可以使用RTTI(运行时类型信息)。以下是一个简化的示例,展示如何使用RTTI来查找接口中定义的方法:
pascaluses System.SysUtils, System.Rtti;
type IMyIntf=interface procedure Foo; end;
procedure FindMethodInInterface(const AInterface: IMyIntf);var TypeInfo: PTypeInfo; MethodIndex: Integer;begin TypeInfo :=PTypeInfo(@IMyIntf); MethodIndex :=TypeInfo.MethodCount - 1; // Foo方法在接口中的索引
// 调用Foo方法 AInterface.Foo;end;
// 示例使用var MyObject: IMyIntf;begin // 假设MyObject是一个实现了IMyIntf接口的对象 // MyObject :=TMyClass.Create;
FindMethodInInterface(MyObject);end.
在这个例子中,我们首先获取了`IMyIntf`接口的类型信息,然后通过`MethodCount`属性找到`Foo`方法在接口中的索引。最后,我们直接调用这个方法。
请注意,这个例子假设你已经有一个实现了`IMyIntf`接口的对象。在实际应用中,你需要创建或获取这样一个对象,并将其传递给`FindMethodInInterface`过程。
如何找到Interface中定义的过程/函数索引?可以用RTTI完成吗? 首先,我们需要枚举接口的方法.不幸的是这个程序{$APPTYPE CONSOLE} uses System.SysUtils, System.Rtti; type IMyIntf = interface procedure Foo; end; procedure EnumerateMethods(IntfType: TRttiInterfaceType); var Method: TRttiMethod; begin for Method in IntfType.GetDeclaredMethodsdo Writeln('Name: ' + Method.Name + 'Index: ' + IntToStr(Method.VirtualIndex)); end; var ctx: TRttiContext; begin EnumerateMethods(ctx.GetType(TypeInfo(IMyIntf)) as TRttiInterfaceType); end.
没有输出.
这个问题涉及这个问题:Delphi TRttiType.GetMethods return zero TRttiMethod instances.
如果您读到该问题的底部,则答案表明使用{$M}进行编译将导致发出足够的RTTI.
{$APPTYPE CONSOLE} {$M+} uses System.SysUtils, System.Rtti; type IMyIntf = interface procedure Foo(x: Integer); procedure Bar(x: Integer); end; procedure EnumerateMethods(IntfType: TRttiInterfaceType); var Method: TRttiMethod; begin for Method in IntfType.GetDeclaredMethods do Writeln('Name: ' + Method.Name + 'Index: ' + IntToStr(Method.VirtualIndex)); end; var ctx: TRttiContext; begin EnumerateMethods(ctx.GetType(TypeInfo(IMyIntf)) as TRttiInterfaceType); end.
输出是:
Name: FooIndex: 3 Name: BarIndex: 4
请记住,所有接口都来自IInterface.所以人们可能会期待它的成员出现.但是,似乎IInterface是在{$M-}状态下编译的.似乎这些方法按顺序列举,尽管我没有理由相信这是有保证的.
感谢@RRUZ指出VirtualIndex的存在.
本文共计531个文字,预计阅读时间需要3分钟。
要找到Interface中定义的过程/函数的引用,可以使用RTTI(运行时类型信息)。以下是一个简化的示例,展示如何使用RTTI来查找接口中定义的方法:
pascaluses System.SysUtils, System.Rtti;
type IMyIntf=interface procedure Foo; end;
procedure FindMethodInInterface(const AInterface: IMyIntf);var TypeInfo: PTypeInfo; MethodIndex: Integer;begin TypeInfo :=PTypeInfo(@IMyIntf); MethodIndex :=TypeInfo.MethodCount - 1; // Foo方法在接口中的索引
// 调用Foo方法 AInterface.Foo;end;
// 示例使用var MyObject: IMyIntf;begin // 假设MyObject是一个实现了IMyIntf接口的对象 // MyObject :=TMyClass.Create;
FindMethodInInterface(MyObject);end.
在这个例子中,我们首先获取了`IMyIntf`接口的类型信息,然后通过`MethodCount`属性找到`Foo`方法在接口中的索引。最后,我们直接调用这个方法。
请注意,这个例子假设你已经有一个实现了`IMyIntf`接口的对象。在实际应用中,你需要创建或获取这样一个对象,并将其传递给`FindMethodInInterface`过程。
如何找到Interface中定义的过程/函数索引?可以用RTTI完成吗? 首先,我们需要枚举接口的方法.不幸的是这个程序{$APPTYPE CONSOLE} uses System.SysUtils, System.Rtti; type IMyIntf = interface procedure Foo; end; procedure EnumerateMethods(IntfType: TRttiInterfaceType); var Method: TRttiMethod; begin for Method in IntfType.GetDeclaredMethodsdo Writeln('Name: ' + Method.Name + 'Index: ' + IntToStr(Method.VirtualIndex)); end; var ctx: TRttiContext; begin EnumerateMethods(ctx.GetType(TypeInfo(IMyIntf)) as TRttiInterfaceType); end.
没有输出.
这个问题涉及这个问题:Delphi TRttiType.GetMethods return zero TRttiMethod instances.
如果您读到该问题的底部,则答案表明使用{$M}进行编译将导致发出足够的RTTI.
{$APPTYPE CONSOLE} {$M+} uses System.SysUtils, System.Rtti; type IMyIntf = interface procedure Foo(x: Integer); procedure Bar(x: Integer); end; procedure EnumerateMethods(IntfType: TRttiInterfaceType); var Method: TRttiMethod; begin for Method in IntfType.GetDeclaredMethods do Writeln('Name: ' + Method.Name + 'Index: ' + IntToStr(Method.VirtualIndex)); end; var ctx: TRttiContext; begin EnumerateMethods(ctx.GetType(TypeInfo(IMyIntf)) as TRttiInterfaceType); end.
输出是:
Name: FooIndex: 3 Name: BarIndex: 4
请记住,所有接口都来自IInterface.所以人们可能会期待它的成员出现.但是,似乎IInterface是在{$M-}状态下编译的.似乎这些方法按顺序列举,尽管我没有理由相信这是有保证的.
感谢@RRUZ指出VirtualIndex的存在.

