delphi rtti 访问记录中的数组

delphi rtti access array in record

我想枚举记录的字段信息(名称,类型,...)。 RTTI 传递字段名称但类型为空(nil)!我怎样才能得到这些信息?

记录:

 foo = record
  bar : array[0..5] of char;
 end;

枚举:

  for var f : TRttiField  in TRTTIContext.Create.GetType(TypeInfo(foo)).GetFields do
  begin
    OutputDebugString(PWideChar(f.Name + ' :: ' + f.FieldType.ToString())); ///fieldtype is nil??!
  end;

RTTI 系统仅适用于预定义类型。定义字段类型“on-the-fly”不会生成 RTTI 信息。因此,改为像这样声明数组类型:

type
  TChar5Arr = array[0..5] of Char;

  foo = record
    bar : TChar5Arr;
  end;

您将获得更多信息:

name: bar
type: TChar5Arr
value: (array)    //is not retrieved using GetValue

根据 Delphi 的文档:

Querying for Type Information

In Delphi, type information is emitted only for types and not for global unit routines, constants, or variables. This means that the obtained TRttiContext can be used only to query for information about the declared types.