delphi:如何将TGUID转换为PTypeInfo指针?

时间:2012-04-25 05:27:05

标签: delphi interface delphi-2010 rtti

我有一个TGUID变量,我希望通过Rtti将其“转换”为描述该接口的PTypeInfo ..

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

.. transform AGUID to PTypeInfo ..

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

Delphi 2010 +

1 个答案:

答案 0 :(得分:6)

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;