记录中的Delphi RTTI访问阵列

发布于 2025-01-30 18:07:44 字数 384 浏览 5 评论 0原文

我想列举记录的字段信息(名称,类型,...)。 RTTI提供字段名称,但键入为null(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;

I want to enumerate field information (name, type, …) of a record. RTTI delivers field name but type is null(nil)! How can I get this information?

Record:

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

Enumeration:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2025-02-06 18:07:45

根据Delphi的文档:

在Delphi中,类型信息仅适用于类型,而不是用于全局单元例程,常数或变量。这意味着获得的trtticontext仅可用于查询有关声明类型的信息

Per Delphi's documentation:

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.

绿光 2025-02-06 18:07:44

RTTI系统仅适用于预定义的类型。定义字段类型“即时”不会生成RTTI信息。因此,请声明这样的数组类型:

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

  foo = record
    bar : TChar5Arr;
  end;

您将获得更多信息:

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

The RTTI system only works with predefined types. Defining field types "on-the-fly" does not generate RTTI information. So, declare the array type like this instead:

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

  foo = record
    bar : TChar5Arr;
  end;

And you will get some more info:

name: bar
type: TChar5Arr
value: (array)    //is not retrieved using GetValue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文