教程-Supports判断接口(Instance)是否支持

本文介绍了一个用于Delphi的命令可见性控制器函数,该函数通过遍历多个执行者并利用Supports函数来判断特定命令是否可见。Supports函数位于SysUtils单元,用于检查接口实例是否支持指定接口。

 

 1 function TCommandEnabledController.GetCommandVisible(const ACommandName: string): Boolean;
 2 var
 3   I: Integer;
 4   //定义接口接收者
 5   oCommandVisibleExecutor: ICommandVisibleExecutor;
 6 begin
 7   Result := True;
 8   for I := 0 to FExecutors.Count - 1 do
 9   begin
10     //判断接口是否支持
11     if Supports(FExecutors[I], ICommandVisibleExecutor, oCommandVisibleExecutor) then
12     begin
13       //使用接口接收者
14       if not oCommandVisibleExecutor.CommandVisible(ACommandName) then
15       begin
16         Result := False;
17         Break;
18       end;
19     end;
20   end;
21 end;

 

delphi中的函数Supports位于SysUtils单元

定义如下:

1 { Interface support routines }
2 
3 function Supports(const Instance: IInterface; const IID: TGUID; out Intf): Boolean; overload;
4 function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean; overload;
5 function Supports(const Instance: IInterface; const IID: TGUID): Boolean; overload;
6 function Supports(const Instance: TObject; const IID: TGUID): Boolean; overload;
7 function Supports(const AClass: TClass; const IID: TGUID): Boolean; overload;

实现如下

 1 { Interface support routines }
 2 
 3 function Supports(const Instance: IInterface; const IID: TGUID; out Intf): Boolean;
 4 begin
 5   Result := (Instance <> nil) and (Instance.QueryInterface(IID, Intf) = 0);
 6 end;
 7 
 8 function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean;
 9 var
10   LUnknown: IUnknown;
11 begin
12   Result := (Instance <> nil) and
13             ((Instance.GetInterface(IUnknown, LUnknown) and Supports(LUnknown, IID, Intf)) or
14              Instance.GetInterface(IID, Intf));
15 end;
16 
17 function Supports(const Instance: IInterface; const IID: TGUID): Boolean;
18 var
19   Temp: IInterface;
20 begin
21   Result := Supports(Instance, IID, Temp);
22 end;
23 
24 function Supports(const Instance: TObject; const IID: TGUID): Boolean;
25 var
26   Temp: IInterface;
27 begin
28   Result := Supports(Instance, IID, Temp);
29 end;
30 
31 function Supports(const AClass: TClass; const IID: TGUID): Boolean;
32 begin
33   Result := AClass.GetInterfaceEntry(IID) <> nil;
34 end;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值