I am trying to obtain the values of the fields in a class using GetFieldProps
Initially I use GetClassLayout to get an array of FieldDef tokens, then enumerate them using GetFieldProps.
The result of each GetFieldProps call returns S_OK and does populate the name of the field in szField.
I am calling trying this in an ExceptionThrown callback, and when attempting this for the exception, it returns field names of _className, _message etc., but ppValue is always empty, as is pcchValue.
How do I get the value of fields in a class?
This is a sample of my current approach (with declarations etc. removed):
COR_FIELD_OFFSET* fieldTokensAndOffsets = new COR_FIELD_OFFSET[fieldArraySize];
m_info->GetClassLayout(
classId,
fieldTokensAndOffsets,
fieldArraySize,
&a,
&b);
for (int i = 0; i < fieldArraySize - 1; i++) {
auto rid = fieldTokensAndOffsets[i].ridOfField;
MetaDataImport->GetFieldProps(
rid,
&mb,
fieldName,
100,,
&pchField,
&pdwAttr,
&ppvSigBlob,
&pcbSigBlob,
&pdwCPlusTypeFlag,
&pValue,
&pcchValue
);
}
GetFieldPropsonly provides static information, sopValue&pcchValueare probably only relevant for fields representing constants.To get the actual value from an instance, I believe you need to use the
COR_FIELD_OFFSET.ulOffsetreturned byGetClassLayoutto get the memory location relative to the ObjectID (ObjectID is a pointer to the actual instance) and you can get the required size/interpretation by parsing the signature thatGetFieldPropsstores inppvSigBlobandpcbSigBlob(the format of which is defined in ECMA-335 Partition II Section 23.2.4).GetClassLayoutto find the location of it's component fields.GetStringLayoutandGetArrayObjectInforespectfully.