如果类中的字段是Record,如何确定使用Rtti [英] How to determine using Rtti, if a field from a class is a Record

查看:99
本文介绍了如果类中的字段是Record,如何确定使用Rtti的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个RttiHelper类,该类可以检索一个类的所有字段名称。该过程成功确定了字段是对象还是数组,但无法确定字段是Record。遵循代码:

I coded a RttiHelper class, that among other things, can retrieve all field's names of a class. The procedure succesfully determines if a field is an object or an array, but it cannot determine if a field is a Record. Follows code:

unit Form1
interface
uses RttiHelper;
type
  tMyRec = Record
   ...
  end;      
...
implementation
var MyRec : tMyRec;
procedure FormCreate (Sender: tObject);
begin
  SetRec (@MyRec);
end;

unit RttiHelper
interface
type
  tObjRec =  class
   Rec    : Pointer;
  end; 
  ...
  tRttiHelperClass = class (tObject)
  private  
    fObjRec: tObjRec;
  ...
    procedure GetFieldsNames;
  ...
  public  
  ...
    procedure SetRec (aRec: Pointer);
  ...
  published
  ...
    constructor Create (aOwner: tComponent);
  ...
  end;
implementation
constructor tRttiHelperClass.Create (aOwner: tComponent);
begin
  fCtxt := tRttiContext.Create;
end;
procedure tRttiHelperClass.SetRec (aRec: Pointer);
begin
  private
    fObjectRec.Rec := aRec;
    procedure GetFieldsNames;
end;
procedure tRttiHelperClass.GetFieldsNames;
var f      :  Word    ;
    fields : tRttiType;
begin
  with fRttiContext do begin
        RttiType := GetType (fObjRec.ClassType);
        Fields   := RttiType.GetFields;
         for f := Low (fields) to High (fields) fo begin
             if fields[f].GetValue (tObject (fObjRec^)).IsArray  then // this works
                ...
             if fields[f].GetValue (tObject (fObjRec^)).IsObject then // this works
                ...
             if fields[f].GetValue (tObject (fObjRec^)).IsRecord then // "undeclared identifier IsRecord"
                ...
  end;
end;
end.

我知道要使用Records,必须使用tRttiRecordType,但找不到正确的方法来做到这一点。如何确定某些字段是否为记录的正确代码?
谢谢。

I know that in order to work with Records, I must use tRttiRecordType, but I couldn't find the right way to do this. How is the correct code for determining if some field is a Record? Thanks.

推荐答案

请尝试以下操作:

if fields[f].FieldType.IsRecord then

来自< a href = http://docwiki.embarcadero.com/Libraries/en/System.Rtti.TRttiField.FieldType rel = nofollow noreferrer> System.Rtti.TRttiField.FieldType 和 System.Rtti.TRttiType.IsRecord

现在,这里的根本问题是您无法从无类型的指针中解析记录字段。为了做到这一点,请将您的记录作为TValue传递。

Now the underlying problem here is you cannot resolve the record fields from an untyped pointer. In order to do something like that, pass your record as a TValue.

procedure SetRec( aRec: TValue);

这样称呼:

SetRec(TValue.From(MyRec));

有关如何完成此操作并解决记录内容的更完整的教程,请参见将记录转换为序列化表单数据以通过HTTP发送

For a more complete tutorial how to do this and resolve the contents of a record, see Convert Record to Serialized Form Data for sending via HTTP.

传递TValue有效以及类和其他类型。

Passing TValue works with classes and other types as well.

这篇关于如果类中的字段是Record,如何确定使用Rtti的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆