我有这些接口声明:
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;