开发者

List the Record's element\fields

开发者 https://www.devze.com 2023-01-29 08:31 出处:网络
Is it possible to get the list of a record\'s elements or fields similar to a list of 开发者_StackOverflowa class\'s published properties via type info ?

Is it possible to get the list of a record's elements or fields similar to a list of 开发者_StackOverflowa class's published properties via type info ?

Thanks !


Depends of your delphi version, if you are using delphi 2010 o newer you can use the New rtti enhancements.

check this code

program ProjectTestRtti;

{$APPTYPE CONSOLE}

uses
  Rtti,
  SysUtils;

type
  MyRecord=record
   Field1 : integer;
   Field2 : boolean;
   Field3 : string;
  end;

var
 ctx   : TRttiContext;
 t     : TRttiType;
 field : TRttiField;
begin
 try
     ctx := TRttiContext.Create;
     for field in ctx.GetType(TypeInfo(MyRecord)).GetFields do
     begin
       t := field.FieldType;
       writeln(Format('Field : %s : Type : %s',[field.Name,field.FieldType.Name]));
     end;
 except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
 end;

  Readln;
end.
0

精彩评论

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