Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.TypeLoadException:无法加载类型';System.Func`2';_C# - Fatal编程技术网

C# System.TypeLoadException:无法加载类型';System.Func`2';

C# System.TypeLoadException:无法加载类型';System.Func`2';,c#,C#,我的代码中出现System.TypeLoadException,描述如下: 无法从程序集'mscorlib,版本=2.0.0.0,区域性=中立,PublicKeyToken=b77a5c561934e089'加载类型'System.Func'2' 下面是我在错误附近所做的基本操作,san try catch和其他对逻辑不重要的东西: // assembly is an Assembly object // derived is of type Derived, which is declared

我的代码中出现System.TypeLoadException,描述如下:

无法从程序集'mscorlib,版本=2.0.0.0,区域性=中立,PublicKeyToken=b77a5c561934e089'加载类型'System.Func'2'

下面是我在错误附近所做的基本操作,san 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
    }
}
以下是衍生工具的基本知识:

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

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

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

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

我的问题是可执行文件和Dll同名

  • SomeProject.dll
  • SomeProject.exe

您的环境是什么?这是桌面应用程序吗?windows应用商店/运行时应用程序?网络应用?你的应用程序针对的是.net framework的哪个版本?在哪里定义了
Bar()
?你们的思路是对的<代码>派生的
基础
是在不同的项目中定义的,我刚刚意识到它们针对的是不同版本的.Net。看我的回答不正确!类型
Func`2
(或C#语法中的
Func
)或命名类型参数的
Func`2[T,TResult]
,在.NET3.5中是新的。在.NET2.0中,这些代理中只有
操作'1
。常规
Func
Action
代表随Linq一起来。欢迎光临。例如,.NET2类型
List
有一个方法
ConvertAll
,该方法对应于Linq的
Select
方法。
List.ConvertAll
方法接受一个委托
Converter
,它在签名和返回类型中完全等同于后面的
Func
类型。