private static string GetTypeName(CorApi.Portable.IMetaDataImport importer, int tk) { unsafe { string result = string.Empty; // Get the base type name MetadataToken token = new MetadataToken(tk); int size; int pdwTypeDefFlags; int ptkExtends; if (token.IsOfType(MetadataTokenType.TypeDef)) { importer.GetTypeDefProps(token, IntPtr.Zero, 0, out size, out pdwTypeDefFlags, out ptkExtends ); var sbBaseName = stackalloc char[size]; importer.GetTypeDefProps(token, (IntPtr)sbBaseName, size, out size, out pdwTypeDefFlags, out ptkExtends ); result = new string(sbBaseName, 0, size - 1); } else if (token.IsOfType(MetadataTokenType.TypeRef)) { // Some types extend TypeRef 0x02000000 as a special-case // But that token does not exist so we can't get a name for it if (token.Index != 0) { int resolutionScope; importer.GetTypeRefProps(token, out resolutionScope, IntPtr.Zero, 0, out size ); var sbBaseName = stackalloc char[size]; importer.GetTypeRefProps(token, out resolutionScope, (IntPtr)sbBaseName, size, out size ); result = new string(sbBaseName, 0, size - 1); } } // Note the base type can also be a TypeSpec token, but that only happens // for arrays, generics, that sort of thing. In this case, we'll leave the base // type name stringbuilder empty, and thus know it's not an enum. return(result); } }