TypeLoadException如何发生?

问题描述:

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 & stackMark)在 System.Reflection.Assembly.GetTypes() 在NameSpace.SurrogateSelector..ctor() 在 d:\项目\ Esfand \ Esfand \类名。 cs:line 2661}TypeLoadException如何发生?

+0

“NameSpace”位于同一个模块中(该过程位于该名称空间中) – Behrooz 2010-03-18 11:27:14

+0

什么是异常消息?是否有InnerException? – itowlson 2010-03-18 11:28:00

+0

如果您显示完整代码 – Andrey 2010-03-18 11:28:13

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

例如,如果你定义的方法一样,

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

确保您在使用它的[DllImport("user32.dll")]。每个TypeLoadException都应该告诉没有找到实现的方法。