List the Record's element\fields
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.
精彩评论