public TypeReference GetTypeReferenceProperties(int token) { object metadataObject = null; if (this.metadataObjectsCache.TryGetValue(token, out metadataObject)) { return (TypeReference)metadataObject; } // The TypeRef's name will be stored in this array. The 1024 is a "magical number", seems like a type's name can be maximum this long. The corhlpr.h also defines a suspicious constant like this: #define MAX_CLASSNAME_LENGTH 1024 var typeName = new char[1024]; // Number of how many characters were filled in the typeName array. var nameLength = 0; // TypeRef's flags. var resolutionScope = 0; // Get A pointer to the scope in which the reference is made. This value is an AssemblyRef or ModuleRef token. var hresult = this.import.GetTypeRefProps(token, ref resolutionScope, typeName, typeName.Length, ref nameLength); if (hresult != 0) { Marshal.ThrowExceptionForHR(hresult); } // supress names "" & "\0"; if (nameLength <= 1) { // return null for this, we do not need to know about empty base return null; } // Get the TypeRef's name. var fullTypeName = new string(typeName, 0, nameLength - 1); var typeRefProp = new TypeReference() { Token = token, FullName = fullTypeName }; this.metadataObjectsCache[token] = typeRefProp; // seems it expects only AssemblyRef, not ModuleRef // typeRefProp.Module = this.GetModuleReferenceProperties(resolutionScope); return typeRefProp; }
public TypeDefinition GetTypeDefByTypeRef(TypeReference typeRef) { // todo: if you can't find TypeDef by typeRef it means you need to iterate all modules (EnumModules) and look there. var mappedCppType = string.Empty; if (!CsTypesToCppTypes.Map.TryGetValue(typeRef.FullName, out mappedCppType)) { mappedCppType = typeRef.FullName; } if (!mappedCppType.StartsWith("System.")) { var typeDefTokenForTypeRef = 0; this.import.FindTypeDefByName(mappedCppType, 0, ref typeDefTokenForTypeRef); if (typeDefTokenForTypeRef != 0) { return this.GetTypeDefinitionProperties(typeDefTokenForTypeRef); } } return null; }