开发者

How to get access field in Delphi using RTTI?

开发者 https://www.devze.com 2022-12-08 20:26 出处:网络
Consider 开发者_如何学Cthe following: TFieldType = class fValue: string; end; TMainClass = class private

Consider 开发者_如何学Cthe following:

TFieldType = class
  fValue: string;
end;

TMainClass = class
private
  Ffield: TFieldType;
public
  function GetValue: string;
end;

In TMainClass.GetValue I'm tryin get values of TMainClass fields:

function TMainClass.GetValue;
begin
  vCtx := TRTTIContext.Create;
  vType := vCtx.GetType(Self.ClassInfo);
  for vField in vType.GetFields do
    vField.GetValue(
        //Here's the trouble, because i don't know how to get the instance
    );

May be there are another ways of getting values of fields which are the instances of another classes?


You have to pass the instance as the a parameter of GetValue like

vField.GetValue(self);

For a better understanding of Rtti read the remarkable articles about RTTI by Robert Love. For this problem specialy this one about Properties and Fields.

0

精彩评论

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