Delphi中如何检测一个类是否正确实现了特定接口?

更新于
2026-10-05 06:49:10
1阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1125个文字,预计阅读时间需要5分钟。

通过`TObject.GetInterface`方法可以获取对象实例实现的接口。前提是实例化对象后才能执行`GetInterface`方法。下面是一个示例:

csharpusing System;

public interface IMyInterface{ void DoSomething();}

public class MyClass : IMyInterface{ public void DoSomething() { Console.WriteLine(Doing something...); }}

class Program{ static void Main() { MyClass obj=new MyClass(); Type interfaceType=obj.GetInterface(IMyInterface); if (interfaceType !=null) { Console.WriteLine(Object implements IMyInterface); Console.WriteLine(Interface offset: + (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9)); } }}

通过TObject.GetInterface可以获得对象的实例实现某个接口,前提条件是必须实例化对象后才能运行GetInterface

下面的方法可获取类是否实现了某个接口,并返回接口的偏移:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 function FindInterface(AClass: TClass; GUID:TGUID; var Offset:NativeInt): Boolean ; var i : integer ; InterfaceTable: PInterfaceTable; InterfaceEntry: PInterfaceEntry; begin while Assigned(AClass) do begin InterfaceTable := AClass . GetInterfaceTable; if Assigned(InterfaceTable) then begin for i := 0 to InterfaceTable . EntryCount- 1 do begin InterfaceEntry := @InterfaceTable . Entries[i]; if InterfaceEntry . IID=GUID then begin Offset:=InterfaceEntry . IOffset; Exit( True ); end ; end ; end ; AClass := AClass . ClassParent; end ; end ;

 下面我们看下通过偏移量的快速获得对象的接口,以及通过接口快速获取对象:

快速获取对象的接口:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 type TSomeObject= class (TXXX, ISomeInterface) ...... end ; var FItemObjectOffset:NativeInt; //获取偏移量 FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset) var P:TSomeObject; Intf:ISomeInterface; .................................... //通过对象直接获取接口 PNativeInt(@Intf)^:=PNativeInt(@P)^+ FItemObjectOffset; Intf . _AddRef;

  

快速通过接口获取对象

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 type TSomeObject= class (TXXX, ISomeInterface) ...... end ; var FItemObjectOffset:NativeInt; //获取偏移量 FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset) var P:TSomeObject; Intf:ISomeInterface; .................................... //通过接口获取对象 P:=TSomeObject( Pointer (PNativeInt(@Intf)^- FItemObjectOffset));

www.cnblogs.com/hezihang/p/5735897.html

本文共计1125个文字,预计阅读时间需要5分钟。

通过`TObject.GetInterface`方法可以获取对象实例实现的接口。前提是实例化对象后才能执行`GetInterface`方法。下面是一个示例:

csharpusing System;

public interface IMyInterface{ void DoSomething();}

public class MyClass : IMyInterface{ public void DoSomething() { Console.WriteLine(Doing something...); }}

class Program{ static void Main() { MyClass obj=new MyClass(); Type interfaceType=obj.GetInterface(IMyInterface); if (interfaceType !=null) { Console.WriteLine(Object implements IMyInterface); Console.WriteLine(Interface offset: + (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9)); } }}

通过TObject.GetInterface可以获得对象的实例实现某个接口,前提条件是必须实例化对象后才能运行GetInterface

下面的方法可获取类是否实现了某个接口,并返回接口的偏移:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 function FindInterface(AClass: TClass; GUID:TGUID; var Offset:NativeInt): Boolean ; var i : integer ; InterfaceTable: PInterfaceTable; InterfaceEntry: PInterfaceEntry; begin while Assigned(AClass) do begin InterfaceTable := AClass . GetInterfaceTable; if Assigned(InterfaceTable) then begin for i := 0 to InterfaceTable . EntryCount- 1 do begin InterfaceEntry := @InterfaceTable . Entries[i]; if InterfaceEntry . IID=GUID then begin Offset:=InterfaceEntry . IOffset; Exit( True ); end ; end ; end ; AClass := AClass . ClassParent; end ; end ;

 下面我们看下通过偏移量的快速获得对象的接口,以及通过接口快速获取对象:

快速获取对象的接口:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 type TSomeObject= class (TXXX, ISomeInterface) ...... end ; var FItemObjectOffset:NativeInt; //获取偏移量 FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset) var P:TSomeObject; Intf:ISomeInterface; .................................... //通过对象直接获取接口 PNativeInt(@Intf)^:=PNativeInt(@P)^+ FItemObjectOffset; Intf . _AddRef;

  

快速通过接口获取对象

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 type TSomeObject= class (TXXX, ISomeInterface) ...... end ; var FItemObjectOffset:NativeInt; //获取偏移量 FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset) var P:TSomeObject; Intf:ISomeInterface; .................................... //通过接口获取对象 P:=TSomeObject( Pointer (PNativeInt(@Intf)^- FItemObjectOffset));

www.cnblogs.com/hezihang/p/5735897.html