您的位置:首页 > 其它

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

2015-08-31 14:03 288 查看
function TCommandEnabledController.GetCommandVisible(const ACommandName: string): Boolean;
var
I: Integer;
//定义接口接收者
oCommandVisibleExecutor: ICommandVisibleExecutor;
begin
Result := True;
for I := 0 to FExecutors.Count - 1 do
begin
//判断接口是否支持
if Supports(FExecutors[I], ICommandVisibleExecutor, oCommandVisibleExecutor) then
begin
//使用接口接收者
if not oCommandVisibleExecutor.CommandVisible(ACommandName) then
begin
Result := False;
Break;
end;
end;
end;
end;


delphi中的函数Supports位于SysUtils单元

定义如下:

{ Interface support routines }

function Supports(const Instance: IInterface; const IID: TGUID; out Intf): Boolean; overload;
function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean; overload;
function Supports(const Instance: IInterface; const IID: TGUID): Boolean; overload;
function Supports(const Instance: TObject; const IID: TGUID): Boolean; overload;
function Supports(const AClass: TClass; const IID: TGUID): Boolean; overload;


实现如下:

{ Interface support routines }

function Supports(const Instance: IInterface; const IID: TGUID; out Intf): Boolean;
begin
Result := (Instance <> nil) and (Instance.QueryInterface(IID, Intf) = 0);
end;

function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean;
var
LUnknown: IUnknown;
begin
Result := (Instance <> nil) and
((Instance.GetInterface(IUnknown, LUnknown) and Supports(LUnknown, IID, Intf)) or
Instance.GetInterface(IID, Intf));
end;

function Supports(const Instance: IInterface; const IID: TGUID): Boolean;
var
Temp: IInterface;
begin
Result := Supports(Instance, IID, Temp);
end;

function Supports(const Instance: TObject; const IID: TGUID): Boolean;
var
Temp: IInterface;
begin
Result := Supports(Instance, IID, Temp);
end;

function Supports(const AClass: TClass; const IID: TGUID): Boolean;
begin
Result := AClass.GetInterfaceEntry(IID) <> nil;
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: