pragmatic_programmer
pragmatic_programmer

Reputation: 3756

delphi: how to transform a TGUID to a PTypeInfo pointer?

I have a TGUID variable, and I want to "transform" it via Rtti to the PTypeInfo that describes that interface..

AGUID := StringToGUID('{19BB9F78-1FB1-4B0F-B691-82EE5CD7A941}');

.. transform AGUID to PTypeInfo ..

AInterface = GlobalContainer.Resolve( <PTypeInfo> expected);

Delphi 2010+

Upvotes: 4

Views: 596

Answers (1)

Ondrej Kelle
Ondrej Kelle

Reputation: 37221

function GetInterfaceTypeInfo(const GUID: TGUID): PTypeInfo;
var
  Ctx: TRttiContext;
  AType: TRttiType;
begin
  Result := nil;
  Ctx := TRttiContext.Create;
  try
    for AType in Ctx.GetTypes do
      if (AType.TypeKind = tkInterface) and IsEqualGUID(GetTypeData(AType.Handle)^.Guid, GUID) then
      begin
        Result := AType.Handle;
        Break;
      end;
  finally
    Ctx.Free;
  end;
end;

Upvotes: 6

Related Questions