【发布时间】:2021-04-17 04:56:06
【问题描述】:
在下文中,Obtain() 方法有效,但 GetAs() 方法对调用者来说会更整洁。
但是,我不知道如何将接口作为通用参数传递并获取其 GUID。
声明:
TInterfaceRegistry = class
private
fRegistry: TDictionary<TGUID, IInterface>;
public
...
procedure Register(const IID: TGUID; IntfObj: IInterface; const Replace: Boolean = False);
function Obtain(const IID: TGUID; out IntfObj): Boolean;
function GetAs<I: IInterface>(): I;
end;
实施:
function TInterfaceRegistry.Obtain(const IID: TGUID; out IntfObj): Boolean;
var
Found: IInterface;
begin
Result := fRegistry.TryGetValue(IID, Found);
if Result then
if not Supports(Found, IID, IntfObj) then
raise EBatSoft.Create(SEBatSoftBug);
end;
function TInterfaceRegistry.GetAs<I>(): I;
begin
if not Obtain(I, Result) then
raise EBatSoft.Create(SEDoesNotImplement);
end;
调用它看起来像这样......
IntfReg.Register(IMyIntf, TMyImpl.Create);
获得实施:
var
Intf: IMyIntf;
begin
// Less type-safe (can pass anything into 2nd parameter)
if IntfReg.Obtain(IMyIntf, Intf) then
...
// Fully type-safe, and simple
Intf := IntfReg.GetAs<IMyIntf>();
【问题讨论】: