[英]System.TypeLoadException: Could not load type 'x' from assembly 'x'?
[英]System.TypeLoadException: Could not load type 'System.Func`2'
我的代码中出现System.TypeLoadException并带有以下描述:
Could not load type 'System.Func`2' from assembly 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
基本上,这是我在错误附近所做的工作,没有try-catch以及其他与逻辑无关的东西:
// assembly is an Assembly object
// derived is of type Derived, which is declared in assembly
// this line works fine
derived = assembly.CreateInstance(derivedClassName, true) as Base;
// this is fine
derived.Foo();
// Exception happens here
derived.Bar();
这是Base的基础知识:
public abstract class Base : SomeOtherClass
{
protected Base() : base() {}
public void Foo()
{
// do stuff
}
}
这是Derived的基础知识:
public class Derived : Base
{
// overrides SomeOtherClass.Foo(), which is the only abstract method
protected override void Foo()
{
// do stuff
}
}
嗯,发现了问题。 Base
和Derived
是在两个不同的项目中定义的,这些项目针对两个不同的.NET版本。 有关更多信息,请参见此问题 。
唯一缺少此泛型类型的mscorlib
是2.0之前的版本。 检查您是否没有引用任何这些版本。
我在C ++ / CLI项目中遇到了同样的问题。 我无法在C#项目中使用C ++ / CLI DLL中的类。 我可以从代码(Assembly.LoadFrom)加载程序集,并且可以使用Reflection(Assembly.CreateInstance)创建类的实例,但是如果尝试在C#代码中使用类,我仍然会收到TypeLoadException。
我的问题是Executable和Dll具有相同的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.