开发者

How to Traverse an fields of Record in delphi2010?

I want to read fields of a record to a string list or an array of edit controls. My code is error, please fix it; and how to Write traversal the fields.

type
  TItem = record
   a : string[20];
   b : word;
   c  : word;
  end;
var
开发者_如何学运维  Form1: TForm1;

implementation
 uses rtti;
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  rttiContext: TRttiContext;
  rttiType: TRttiType;
  fields: TArray<TRttiField>;
  item: TItem;
  i:word;
begin
  item.a:='hello';
  item.b:=123;
  item.c:=456;
  rttiType := rttiContext.GetType(TypeInfo(TItem));
  fields := rttiType.GetFields;
   for i := low(fields) to high(fields) do
   begin
    ShowMessage(fields[i].GetValue(@item).AsString);
   end;

end;

end.


I had to change string[20] to string (I do not know why and I do not know how to get it to work with string[20]). And change AsString to ToString. This is tested on Delphi XE and Delphi 2010.

type
    TItem = record
     a : string;
     b : word;
     c  : word;
    end;

procedure TForm3.FormCreate(Sender: TObject);
var
    rttiContext: TRttiContext;
    rttiType: TRttiType;
    fields: TArray<TRttiField>;
    item: TItem;
    i:word;
begin
    item.a:='hello';
    item.b:=123;
    item.c:=456;
    rttiType := rttiContext.GetType(TypeInfo(TItem));
    fields := rttiType.GetFields;
     for i := low(fields) to high(fields) do
     begin
        ShowMessage(fields[i].GetValue(@item).ToString);
     end;
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜