2014-07-22 187 views
0

我得到一个System.TypeLoadException在我的代码与下面的描述:System.TypeLoadException:未能加载类型“System.Func`2”

Could not load type 'System.Func`2' from assembly 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

这里基本上是我在做什么错误附近,SANS的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(); 

这里是基地的基础知识:

public abstract class Base : SomeOtherClass 
{ 
    protected Base() : base() {} 

    public void Foo() 
    { 
     // do stuff 
    } 
} 

这里是派生的基本知识:

public class Derived : Base 
{ 
    // overrides SomeOtherClass.Foo(), which is the only abstract method 
    protected override void Foo() 
    { 
     // do stuff 
    } 
} 
+0

什么是你的环境?这是一个桌面应用程序? Windows商店/运行时应用程序? Web应用程序?您的应用定位了哪个版本的.net框架? – Tim

+0

定义了Bar()吗? –

+0

你们是在正确的轨道上。 “衍生”和“基础”是在不同的项目中定义的,我只是意识到它们针对不同版本的.Net。看到我的回答 – wlyles

回答

0

啊,发现了问题。 BaseDerived定义在两个独立的项目中,它们针对两个不同版本的.NET。有关更多信息,请参阅this question

0

缺少此通用类型的唯一mscorlib是2.0之前的版本。检查你是否没有引用任何这些版本。

+0

错误! .NET 3.5中新增了''Func'2''(或''Func <,>'用C#语法)或''Func'2 [T,TResult]'来命名类型参数。在.NET 2.0中,这些代表中只有一个是“Action'1”。一般的'Func'和'Action'代表与Linq一同出席。 –

+0

你说得对。感谢您纠正我 – Alireza

+1

不客气。例如,.NET 2类型'List <>'有一个方法'ConvertAll <>',它对应于Linq的'Select <,>'方法。 'List <>。ConvertAll <>'方法需要一个委托'Converter ',它的签名和返回类型与后面的'Func '类型完全等价。 –

0

我对C++/CLI项目有同样的问题。我无法在C#项目中使用C++/CLI DLL中的类。我可以从代码(Assembly.LoadFrom)加载程序集,我可以使用Reflection(Assembly.CreateInstance)创建我的类的实例,但如果我尝试在C#代码中使用类,则仍然收到TypeLoadException。

我的问题是可执行文件和Dll有相同的名称。

  • SomeProject.dll
  • SomeProject.exe
相关问题