继承的接口可以用作TDictionary中的键吗?

//! Note the inheritance of the interfaces
ILoggingProvider = interface
  procedure Log(AMessage : string);
end;

IHTTPLoggingProvider = interface(ILoggingProvider)
  function Login(AUserName : string; APassword : string) : boolean
end;

ILiveLoggingProvider = interface(ILoggingProvider)
  function ConnectMonitor : boolean
end;

var
  loggers : TDictionary<ILoggingProvider,TObject>;

...

loggers.add(ILoggingProvider, TSomeLogger.Create());
loggers.add(IHTTPLoggingProvider , TSuperLogger.Create()); //fails!
loggers.add(ILiveLoggingProvider , TAnotherLogger.Create()); //fails!


 ... //and the use them selectively

if loggers.ContainsKey(IHTTPLoggingProvider ) then
  loggers.Items[IHTTPLoggingProvider].Log('Good bye world!');

... //and like this

var

  theLogger : IHTTPLoggingProvider;

if loggers.ContainsKey(IHTTPLoggingProvider ) then
begin
  theLogger := IHTTPLoggingProvider(loggers.Items[IHTTPLoggingProvider]);
  if theLogger.Login('billy', 'bones') then
    theLogger.Log('some message');
end;

最佳答案

根据我的评论,以下是我认为可以实现的方式:

type
  ILoggingProvider = interface
    ['{30598F45-1230-4208-B1A5-E1D2DA8F6D40}']
    procedure Log(AMessage : string);
  end;

  IHTTPLoggingProvider = interface(ILoggingProvider)
    ['{CFA01514-AC44-4E30-971B-115986B37D26}']
    function Login(AUserName : string; APassword : string) : boolean;
  end;

  ILiveLoggingProvider = interface(ILoggingProvider)
    ['{6EA68BEF-8D78-4FDF-AACD-1D164A272758}']
    function ConnectMonitor : boolean;
  end;

  TLoggingProvider = class(TInterfacedObject, ILoggingProvider)
    procedure Log(AMessage : string);
  end;

  THTTPLoggingProvider = class(TInterfacedObject, ILoggingProvider, IHTTPLoggingProvider)
    procedure Log(AMessage : string);
    function Login(AUserName : string; APassword : string) : boolean;
  end;

  TLiveLoggingProvider = class(TInterfacedObject, ILoggingProvider, ILiveLoggingProvider)
    procedure Log(AMessage : string);
    function ConnectMonitor : boolean;
  end;

procedure Main;
var
  loggers : TDictionary<TGUID,ILoggingProvider>;
  theLogger : IHTTPLoggingProvider;
begin
  loggers := TDictionary<TGUID,ILoggingProvider>.Create;
  try
    loggers.add(ILoggingProvider, TLoggingProvider.Create);
    loggers.add(IHTTPLoggingProvider, THTTPLoggingProvider.Create);
    loggers.add(ILiveLoggingProvider, TLiveLoggingProvider.Create);

    if loggers.ContainsKey(IHTTPLoggingProvider) then
      loggers.Items[IHTTPLoggingProvider].Log('Good bye world!');

    if loggers.ContainsKey(IHTTPLoggingProvider) then
    begin
      theLogger := loggers.Items[IHTTPLoggingProvider] as IHTTPLoggingProvider;
      if theLogger.Login('billy', 'bones') then
        theLogger.Log('some message');
    end;
  finally
    loggers.Free;
  end;
end;

关于delphi - 在TDictionary中使用继承的接口(interface)作为键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7769853/

10-10 11:24