0

I try to do a service Locator in Delphi, base on Malcom code I'm trying to get the GUID from an Interface passed as var in a method.

My Container class look like that :

  TContainer = class
  strict private
    FDico: TObjectDictionary<TGUID, IInterface>;
    [...]
  public
    procedure Register<ServiceType: IInterface>(IntfInstance : ServiceType);
    function Get<ServiceType: IInterface>: ServiceType;
  end;

To register a class, I need to do :

TContainer.Instance.Register<IFlying>(TBird.Create);

And I get it by :

var
  F: IFlying;
begin
  F := TContainer.GetInstance.Get<IFlying>;
  F.Fly;
end;

What I want to do is :

TContainer.Instance.Register<IFlying>(TBird); // instead of .Create

Here is the Register class :

procedure TContainer.Register<ServiceType>(IntfInstance: ServiceType);
var
  Guid: TGuid;
begin
  Guid := PTypeInfo(TypeInfo(ServiceType)).TypeData.Guid;
  FDico.Add(Guid, IntfInstance);
end;

And the Get class :

function TContainer.Get<ServiceType>: ServiceType;
var
  Guid: TGuid;
  LInterface : IInterface;
  LSpecificInterface : ServiceType;
begin
  Result := nil;
  Guid := PTypeInfo(TypeInfo(ServiceType)).TypeData.Guid;
  if FDico.TryGetValue(Guid, LInterface) then
  begin
    if Supports(LInterface, Guid, LSpecificInterface) then
    begin
      Result := LSpecificInterface;
    end
    else
      raise EIntfCastError.Create(Format('Service %s not supported', [PTypeInfo(TypeInfo(ServiceType)).Name]));
  end
  else
    raise Exception.Create(Format('Service %s not registered', [PTypeInfo(TypeInfo(ServiceType)).Name]));
end;
Bosshoss
  • 783
  • 6
  • 24
  • 3
    Does this answer your question? [Is it possible to get the value of a GUID on an interface using RTTI?](https://stackoverflow.com/questions/8439246/is-it-possible-to-get-the-value-of-a-guid-on-an-interface-using-rtti) – whosrdaddy Oct 06 '20 at 13:01
  • Would you please show the code of the method where you pass an interface as var, and also the code where you call this method? Edit your question with this information. – fpiette Oct 06 '20 at 13:21
  • Not really the same question, I add the full code of the method – Bosshoss Oct 06 '20 at 14:14
  • Well, you're not telling the whole story here. Do you want the Guid of `IDemoInterface`? – whosrdaddy Oct 06 '20 at 15:02
  • Use `TypeInfo(IDemoInterface)`, not `TypeInfo(aInterface)` – Remy Lebeau Oct 06 '20 at 17:02
  • Your code doesn't make much sense... Why retrieve the TGUID of an interface? The compiler knows it directly, since IDemoInterface is an alias to its TGUID.... – Arnaud Bouchez Oct 06 '20 at 20:29
  • I try to do a simple Service Locator, IDemoInterface is the main interface of all other, my var can be a aFlyInterface, IPrintInterface, all come from IDemoInterface. – Bosshoss Oct 07 '20 at 06:18
  • Seems that you already asked a question related: https://stackoverflow.com/a/63889959/800214. What are you missing from Remy's answer? – whosrdaddy Oct 07 '20 at 10:51
  • I dont get any answer to my last question – Bosshoss Oct 07 '20 at 13:38
  • I added the full code blow – Bosshoss Oct 07 '20 at 13:47
  • @whosrdaddy I can't put the code, it tell me there is to much code for the text I write ... and no your answer don't answer to my question – Bosshoss Oct 08 '20 at 05:57

0 Answers0