Ho questi dichiarazioni di interfaccia:Come posso verificare se un oggetto interfacciato implementa un'altra interfaccia?
IListener = interface
procedure HandleEvent(AEvent: TMyEvent);
end;
IExtendedListener = interface(IListener)
['{85A3456A-D5E5-4F37-ABDD-A75A7B3B494C}'] // required by GetInterface
procedure HandleExtendedEvent(AExtendedEvent: TMyExtendedEvent);
end;
Dato un riferimento un'interfaccia per qualcosa che implementa IListener
, come posso verificare se implementa anche IExtendedListener
? Sto usando il codice sotto il codice perché è un errore in fase di compilazione per fare qualcosa come 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;
dare un'occhiata a http://docwiki.embarcadero.com/Libraries/XE3/en/System.SysUtils.Supports –
tuo il codice non controlla se IExtendedListener eredita da IListener, ma semplicemente che l'istanza referenziata tramite IListener supporta anche IExtendedListener. In nessun modo dice nulla sull'eredità tra le due interfacce. –
'HandleExtendedEvent' ha solo un argomento nella dichiarazione. Perché vuoi passare 2 argomenti? –