-1

I'm wondering if this is possible. I want to get the TypeInfo, passing the type's name as a string.

Something like this:

type
  TSomeValues = record
    ValueOne: Integer;
    ValueTwo: string;
  end;


function ReturnTypeInfo(aTypeName: string): TypeInfo;
begin
    // that's is the issue
end;

procedure Button1Click(Sender: TObject);
var
  _TypeInfo: TypeInfo;
begin
  _TypeInfo := ReturnTypeInfo('TSomeValues');
end;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Leo Bruno
  • 474
  • 3
  • 16

1 Answers1

3

Use the TRttiContext.FindType() method and TRttiType.Handle property, eg:

uses
  ..., System.TypInfo, System.Rtti;

function ReturnTypeInfo(aTypeName: string): PTypeInfo;
var
  Ctx: TRttiContext;
  Typ: TRttiType;
begin
  Typ := Ctx.FindType(aTypeName);
  if Typ <> nil then
    Result := Typ.Handle
  else
    Result := nil;
end;

...

type
  TSomeValues = record
    ValueOne: Integer;
    ValueTwo: string;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  _TypeInfo: PTypeInfo;
begin
  _TypeInfo := ReturnTypeInfo('Unit1.TSomeValues');
  ...
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770