1

我正在尝试使用GetFieldProps获取类中字段的值

最初我GetClassLayout用来获取 FieldDef 令牌数组,然后使用GetFieldProps.

每次调用的结果都会GetFieldProps返回 S_OK并填充.szField

我在ExceptionThrown回调中调用尝试这个,当尝试这个异常时,它返回字段名称_className_message,但ppValue始终为空,原样pcchValue

如何获取类中字段的值?

这是我当前方法的一个示例(删除了声明等):

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
    );
}
4

1 回答 1

1

GetFieldProps only provides static information, so pValue & pcchValue are 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.ulOffset returned by GetClassLayout to 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 that GetFieldProps stores in ppvSigBlob and pcbSigBlob (the format of which is defined in ECMA-335 Partition II Section 23.2.4).

  • If the type is a primitive value type, then the size and interpretation should be self evident (eg. Int32 will be a 4 byte integer).
  • If the type is a reference type, then it will be a native int sized field containing the ObjectID.
  • If the type is an enum, then it will have the size of it's underlying type (ECMA-335 Partition II Section 14.3).
  • If the type is a non-primitive type, then you can use GetClassLayout to find the location of it's component fields.
  • you can get a further break-down of strings and arrays using GetStringLayout and GetArrayObjectInfo respectfully.
于 2018-02-12T21:07:31.080 回答