TypeLoadException是如何发生的?

时间:2010-03-18 11:24:15

标签: .net types 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)at   System.Reflection.Assembly.GetTypes()   在NameSpace.SurrogateSelector..ctor()   在   d:\项目\ Esfand \ Esfand \ classname.cs:行   2661}

1 个答案:

答案 0 :(得分:0)

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

例如,如果您定义类似的方法,

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

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