Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You seem to be misunderstanding the way an instance pointer works. You don't store a pointer to the field, you store a pointer to the class or the record that it's a field of. Object references are pointers already, so no casting is needed there. For records, you need to obtain a pointer to them with the @ symbol.</p> <p>Once you have your pointer, and a TRttiField object that refers to that field, you can call SetValue or GetValue on the TRttiField, and pass in your instance pointer, and it takes care of all the offset calculations for you.</p> <p>In the specific case of arrays, GetValue it will give you a TValue that represents an array. You can test this by calling <code>TValue.IsArray</code> if you want. When you have a TValue that represents an array, you can get the length of the array with <code>TValue.GetArrayLength</code> and retrieve the individual elements with <code>TValue.GetArrayElement</code>.</p> <p><strong>EDIT:</strong> Here's how to deal with record members in a class.</p> <p>Records are types too, and they have RTTI of their own. You can modify them without doing "GetValue, modify, SetValue" like this:</p> <pre><code>procedure ModifyPoint(example: TExampleClass; newXValue, newYValue: integer); var context: TRttiContext; value: TValue; field: TRttiField; instance: pointer; recordType: TRttiRecordType; begin field := context.GetType(TExampleClass).GetField('FPoint'); //TValue that references the TPoint value := field.GetValue(example); //Extract the instance pointer to the TPoint within your object instance := value.GetReferenceToRawData; //RTTI for the TPoint type recordType := context.GetType(value.TypeInfo) as TRttiRecordType; //Access the individual members of the TPoint recordType.GetField('X').SetValue(instance, newXValue); recordType.GetField('Y').SetValue(instance, newYValue); end; </code></pre> <p>It looks like the part you didn't know about is TValue.GetReferenceToRawData. That will give you a pointer to the field, without you needing to worry about calculating offsets and casting pointers to integers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload