IMetaDataImport :: ResolveTypeRef方法的问题

时间:2012-01-14 20:45:36

标签: c# .net-4.0 imetadataimport

我有自己的.NET应用调试器,使用IMetaDataImport interface

当我调用ResolveTypeRef方法时,我总是得到NotImplementedException

ResolveTypeRef的定义是这样的:

[ComImport]
[Guid("....")] //a valid GUID
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[CLSCompliant(false)]
public interface IMetaDataImport {
  void ResolveTypeRef(
    [ComAliasName("mdTypeRef")] mdToken tr,
    [ComAliasName("REFIID")] ref Guid riid,
    [ComAliasName("IUnknown**"), Out, MarshalAs(UnmanagedType.IUnknown)] out object ppIScope,
    [ComAliasName("mdTypeDef*"), Out] out mdToken ptd
  );
  // ... other methods from IMetaDataImport
}

调用方法:

metadataImport.ResolveTypeRef(typeRefToken, ref metadataImportGuid, out metadataObject, out typeDefToken)

通常,该方法应解析System.Exception或System.SystemException类型。

当我将应用程序从.NET 3.5移动到.NET 4时出现问题。

感谢您的回答!

1 个答案:

答案 0 :(得分:2)

Uff,我终于摆脱了这个......解决方案不是调用ResolveTypeRef,而是创建自己的方法来解析类型:

  mdToken ptkResScope;
  uint len;
  metadataImport.GetTypeRefProps(typeRef, out ptkResScope, null, 0, out len);
  StringBuilder sb = new StringBuilder((int)len);
  metadataImport.GetTypeRefProps(typeRef, out ptkResScope, sb, len, out len);
  string className = sb.ToString();
  foreach (loadedModule ) {   // this is quite tricky part ...     
    metadataImportForLoadedModule = GetMetaDataImportForModule(loadedModule);
    metadataImportForLoadedModule.FindTypeDefByName(className, mdToken.Nil, out typeDef);
    if ( typeDef.IsNonNil(CorTokenType.mdtTypeDef) ) {
      return typeDef;
      }
    }

这个想法来自David Broman's CLR Profiling API Blog: Metadata Tokens, Run-Time IDs, and Type Loading(如果你对MetaDataImport感兴趣,那就非常好读。)