我有以下这些接口声明:
IListener = interface
procedure HandleEvent(AEvent: TMyEvent);
end;
IExtendedListener = interface(IListener)
['{85A3456A-D5E5-4F37-ABDD-A75A7B3B494C}'] // required by GetInterface
procedure HandleExtendedEvent(AExtendedEvent: TMyExtendedEvent);
end;
如果我有实现了IListener
接口的某个东西的接口引用,我该如何检查它是否还实现了IExtendedListener
接口?因为像if Listener is IExtendedListener then ...
这样做是编译时错误,所以我使用下面的代码。
TSomeClass.Notify(AEvent: TMyExtendedEvent);
var
Listener: IListener;
ExtListener: IExtendedListener;
Obj: TObject;
begin
for Listener in FListeners do
begin
// works but smells funny
Obj := Listener as TObject;
Obj.GetInterface(IExtendedListener, ExtListener);
if Assigned(ExtListener) then
ExtNotifyee.HandleExtendedEvent(AEvent);
end;
end;
is
。不过如果可以的话会很好。 - Lawrence Barsanti