【问题标题】:how does a TypeLoadException Happen?TypeLoadException 是如何发生的?
【发布时间】:2010-03-18 19:52:31
【问题描述】:
public sealed class SurrogateSelector : System.Runtime.Serialization.SurrogateSelector, System.Runtime.Serialization.ISurrogateSelector
{
    System.Runtime.Serialization.ISerializationSurrogate ISS = System.Runtime.Serialization.FormatterServices.GetSurrogateForCyclicalReference(new SerializationSurrogate());
    public SurrogateSelector()
    {
        foreach (Type t in typeof(NameSpace.ASampleClass).Assembly.GetTypes())
        {
            if (t.Namespace == "NameSpace")
                this.AddSurrogate(t, new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.All), ISS);
        }
    }
}

{System.Reflection.ReflectionTypeLoadException: 无法加载一个或多个 请求的类型。检索 LoaderExceptions 属性了解更多信息 信息。在 System.Reflection.Module._GetTypesInternal(StackCrawlMark& 堆栈标记)在 System.Reflection.Assembly.GetTypes() 在 NameSpace.SurrogateSelector..ctor() 在 D:\Projects\Esfand\Esfand\classname.cs:line 第2661章

【问题讨论】:

  • “NameSpace”在同一个模块中(过程在那个命名空间中)
  • 异常信息是什么?有 InnerException 吗?
  • 显示整个代码会很有帮助
  • 按照说明检索LoaderExceptions 属性以获取更多信息。
  • @João Angelo:它说问题出在 RVA(我有很多 externs),但为什么要停止工作呢?

标签: .net types typeloadexception


【解决方案1】:

确保正确定义了所有外部变量。如果 CLR 找不到您定义的外部方法的实现,它将无法加载类型。

例如,如果你定义了一个方法,

public static extern IntPtr SendMessage(IntPtr w, uint m, IntPtr p1, IntPtr p2);

确保您在其上使用[DllImport("user32.dll")]。每个TypeLoadException 都应告知未找到实现的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2019-09-30
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多