How to access record properties?

心不动则不痛 提交于 2019-12-02 14:10:51

问题


I need to access, record properties, and set/get this property values. Firstly, i want to access properties. But i can't.

What is wrong? Ver : Delphi XE6.

sample code:

type
  TmyRecord = record
  private
    Str : String;
  public
    property StrProp :String read Str;  
  end;


procedure TForm1.Button3Click(Sender: TObject);
var
 c : TRttiContext;
 t : TRttiType;
 field : TRttiField;
 prop : TRttiProperty;
begin
 c := TRttiContext.Create;
 try
   Memo1.Lines.Append('Fields');
   for field in c.GetType(TypeInfo(TMyRecord)).GetFields do
   begin
     t := field.FieldType;
     Memo1.Lines.Append('Field:'+field.Name);
     Memo1.Lines.Append('RttiType:'+t.ClassName);
   end;

   Memo1.Lines.Append('Properties');
   for prop in c.GetType(TypeInfo(TMyRecord)).GetProperties do
   begin
     t := prop.PropertyType;
     Memo1.Lines.Append('Property:'+prop.Name);
     Memo1.Lines.Append('RttiType:'+t.ClassName);
   end;

 finally
   c.Free
 end;

end;

回答1:


Your problem is that there is no RTTI available for record properties as already reported in 2009 but still not fixed (QC#78110).

Edit: And still not fixed in 2017 (RSP-19303).



来源:https://stackoverflow.com/questions/23445775/how-to-access-record-properties

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!