uiInterface=Interface(IInterface)
end; utClass=class(TInterfacedObject) end; ///////////////////////////////////////////////////////////////
function func(const oo:utClass):uiInterface;
begin
result:=nil;
if oo 实现 uiinterface then
result:=oo as uiinterface;
end;
end; utClass=class(TInterfacedObject) end; ///////////////////////////////////////////////////////////////
function func(const oo:utClass):uiInterface;
begin
result:=nil;
if oo 实现 uiinterface then
result:=oo as uiinterface;
end;
IID_frmRSProdSheetTemp = '{763A8157-45BD-480B-BDCB-7A8BC06EDB63}';type
IfrmRSProdSheetTemp = interface
[IID_frmRSProdSheetTemp]
procedure GetPNData;
end;
TfrmRSProdSheetTemp = class(TfrmRSTemp, IProdSheet, IfrmRSProdSheetTemp)
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FIProdSheet: IProdSheet;
function GetProdSheet: IProdSheet;
protected
PrintCalc: Boolean;
procedure GetPNData; virtual;
property ProdSheet: IProdSheet read GetProdSheet implements IProdSheet;
public
{ Public declarations }
end;判断TfrmRSProdSheeTemp 是否支持 IfrmRSProdSheetTemp 接口就用下面下面的方法function GetIfrmRSProdSheetTemp: IfrmRSProdSheetTemp;
begin
Result := nil;
if Supports(TfrmRSProdSheetTemp, IID_frmRSProdSheetTemp)then
Result := TfrmRSProdSheetTemp.Create(nil)
else
ShowMessage('TfrmRSProdSheetTemp 不支持 IfrmRSProdSheetTemp接口');
end;
这个Blog 有更深层的用法
http://hi.baidu.com/cncharles/blog/item/4a061df75e4fc123720eec20.html
function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean; overload;
function Supports(const AClass: TClass; const IID: TGUID): Boolean; overload;C++ syntax:extern PACKAGE bool __fastcall Supports(const System::_di_IInterface Instance, const GUID &IID, void *Intf);
extern PACKAGE bool __fastcall Supports(const System::_di_IInterface Instance, const GUID &IID);
extern PACKAGE bool __fastcall Supports(const System::TObject* Instance, const GUID &IID, void *Intf);
extern PACKAGE bool __fastcall Supports(const System::TObject* Instance, const GUID &IID);extern PACKAGE bool __fastcall Supports(const TMetaClass* AClass, const GUID &IID);DescriptionCall Supports to determine whether the object or interface specified by Instance, or the class specified by AClass, supports the interface identified by the IID parameter. If Instance supports the interface, Supports returns the interface as the Intf parameter and returns true. If AClass supports the interface, Supports does not return an interface, but still returns true. If the interface specified by IID is not supported, Supports returns false.To determine whether the interface specified by IID is supported, Supports calls the QueryInterface method of the supplied interface (or the interface of the supplied object). However, unlike calling QueryInterface directly, Supports lets you pass a nil (Delphi) or NULL (C++) value as the instance.
begin
result:=nil;
if supports(utClass,uiinterface) then
result:=(oo as uiinterface);
end;