title: Rtti accessing fields and properties in complex data structures
body: <p>As already discussed in <a href="https://stackoverflow.com/questions/2771864/rtti-data-manipulation-and-consistency-in-delphi-2010">Rtti data manipulation and consistency in Delphi 2010</a> a consistency between the original data and rtti values can be reached by accessing members by using a pair of TRttiField and an instance pointer. This would be very easy in case of a simple class with only basic member types (like e.g. integers or strings).
  But what if we have structured field types?</p>
  
  <p>Here is an example:</p>
  
  <pre><code>TIntArray = array [0..1] of Integer;
  
  TPointArray = array [0..1] of Point;
  
  TExampleClass = class
    private
      FPoint : TPoint;
      FAnotherClass : TAnotherClass;
      FIntArray : TIntArray;
      FPointArray : TPointArray;
    public  
      property Point : TPoint read FPoint write FPoint; 
      //.... and so on
  end;
  </code></pre>
  
  <p>For an easy access of Members I want to buil a tree of member-nodes, which provides an interface for getting and setting values, getting attributes, serializing/deserializing values and so on.</p>
  
  <pre><code>TMemberNode = class
    private
      FMember : TRttiMember;
      FParent : TMemberNode;
      FInstance : Pointer;
    public
      property Value : TValue read GetValue write SetValue; //uses FInstance
  end;
  </code></pre>
  
  <p>So the most important thing is getting/setting the values, which is done - as stated before - by using the GetValue and SetValue functions of TRttiField.</p>
  
  <p>So what is the Instance for FPoint members? Let's say Parent is the Node for TExample class, where the instance is known and the member is a field, then Instance would be:</p>
  
  <pre><code>FInstance := Pointer (Integer (Parent.Instance) + TRttiField (FMember).Offset);
  </code></pre>
  
  <p>But what if I want to know the Instance for a record property? There is no offset in this case. So is there a better solution to get a pointer to the data?</p>
  
  <p>For the FAnotherClass member, the Instance would be:</p>
  
  <pre><code>FInstance := Parent.Value.AsObject;  
  </code></pre>
  
  <p>So far the solution works, and data manipulation can be done by using rtti or the original types, without losing information.</p>
  
  <p>But things get harder, when working with arrays. Especially the second array of Points. How can I get the instance for the members of points in this case?</p>