C# 什么可能导致 System.TypeLoadException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16086178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:57:41  来源:igfitidea点击:

What could be causing a System.TypeLoadException?

c#visual-studiosshwindows-cesmart-device

提问by J.M.J

I'm developing, with VS2008 using C#, an application for Honeywell Dolphin 6100, a mobile computer with a barcode scanner that uses Windows CE 5.0 like OS.

我正在使用 VS2008 使用 C# 开发 Honeywell Dolphin 6100 的应用程序,这是一款带有条形码扫描仪的移动计算机,使用类似操作系统的 Windows CE 5.0。

I want to add a functionality that can send files from the local device to the distant server. I found the librairy "Tamir.SharpSSH" which can garantee this. I tested the code on a console application and on normal windows forms application and it works perfectly. But when I tried to use the same code on the winCE device, I get a TypeLoadException and I have the error message :

我想添加一个功能,可以将文件从本地设备发送到远程服务器。我找到了可以保证这一点的图书馆“ Tamir.SharpSSH”。我在控制台应用程序和普通的 Windows 窗体应用程序上测试了代码,它运行良好。但是当我尝试在 winCE 设备上使用相同的代码时,我收到一个 TypeLoadException 并且我收到错误消息:

Could not load type 'Tamir.SharpSsh.SshTransferProtocolBase' from assembly 'Tamir.SharpSSH,   
Version=1.1.1.13, Culture=neutral, PublicKeyToken=null'.

the code that I'm use is like below :

我使用的代码如下:

SshTransferProtocolBase sshCp = new Scp(Tools.GlobalVarMeth.hostName, Tools.GlobalVarMeth.serverUserName);
sshCp.Password = Tools.GlobalVarMeth.serverUserpassword;
sshCp.Connect();

string localFile = Tools.GlobalVarMeth.applicationPath + "/" + fileName + ".csv";
string remoteFile = Tools.GlobalVarMeth.serverRemoteFilePath + "/" + fileName + ".csv";

sshCp.Put(localFile, remoteFile);

sshCp.Close();

Any one have any idea on this ? I will be really gratefull !!!

有人对此有任何想法吗?我将不胜感激!!!

采纳答案by Eric Lippert

It could be any number of things. Likely causes are:

它可以是任意数量的东西。可能的原因是:

  • The assembly cannot be found
  • An assembly that your assembly depends upon cannot be found
  • The assembly is found but the type isn't in it
  • The type's static constructor throws an exception
  • 找不到程序集
  • 找不到您的程序集所依赖的程序集
  • 找到了程序集,但类型不在其中
  • 类型的静态构造函数抛出异常

Your best bet is to use the Fusion log viewer to help diagnose it. Documentation is here:

最好的办法是使用 Fusion 日志查看器来帮助诊断。文档在这里:

http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx

(FYI "Fusion" was the code name of the team that designed the assembly loading system; it is somewhat unfortunate that the code name ended up in the file name of the shipped product. The thing should have been called "AssemblyBindingLogViewer.exe" or some such thing.)

(仅供参考“Fusion”是设计程序集加载系统的团队的代号;有点不幸的是,代号最终出现在已发货产品的文件名中。这个东西应该被称为“AssemblyBindingLogViewer.exe”或一些这样的事情。)

回答by Clint

This could be caused by any number of things, MSDN has it said as:

这可能是由多种原因引起的,MSDN 说:

TypeLoadException is thrown when the common language runtime cannot find the assembly, the type within the assembly, or cannot load the type.

当公共语言运行库找不到程序集、程序集中的类型或无法加载类型时,将引发 TypeLoadException。

So it's clear that a type can't be found, either the assembly is missing, the type is missing or there's a clash between runtime configurations.

所以很明显找不到类型,要么缺少程序集,要么缺少类型,要么运行时配置之间存在冲突。

Sometimes the issue can arise because the assembly you're referencing is a different platform type (32bit / 64bit etc) than the one you're consuming from.

有时会出现问题,因为您引用的程序集与您使用的程序集是不同的平台类型(32 位/64 位等)。

I would recommend catching the exception and examining it in more detail to identify what it's having trouble with.

我建议捕获异常并更详细地检查它以确定它有什么问题。



Further to my previous information

进一步了解我之前的信息

Sometimes I've seen this issue arise because (for one reason or another) a referenced assembly can't actually be resolved, even though it's referenced and loaded.

有时我看到这个问题的出现是因为(出于某种原因)引用的程序集实际上无法解决,即使它被引用和加载。

I typically see this when AppDomain boundaries are involved.

当涉及 AppDomain 边界时,我通常会看到这一点。

One way I've found that sometimes resolves the issue (but only if the assembly is already in the AppDomain) is this code snippet:

我发现有时可以解决问题的一种方法(但仅当程序集已经在 AppDomain 中时)是以下代码片段:

AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
{
   return AppDomain.CurrentDomain.GetAssemblies()
      .SingleOrDefault(asm => asm.FullName == e.Name);
}

Like I said, I see this issue when AppDomains get involved and this does seem to solve it when the assembly is indeed already referenced and loaded. I've no idea why the framework fails to resolve the reference itself.

就像我说的,当 AppDomains 参与进来时,我看到了这个问题,当程序集确实已经被引用和加载时,这似乎确实解决了它。我不知道为什么框架无法解析引用本身。

回答by Mike Dimmick

You can get loader log files out of the .NET Compact Framework by enabling some settings in the registry. The Power Toys for .NET Compact Frameworkinclude a tool called NETCFLogging, which sets the registry values for you.

通过启用注册表中的某些设置,您可以从 .NET Compact Framework 中获取加载程序日志文件。该电源玩具.NET Compact Framework中包括一个名为NETCFLogging工具,设置注册表值给你。

The registry values are documented here:

此处记录了注册表值:

http://msdn.microsoft.com/en-us/library/ms229650(v=VS.90).aspx

http://msdn.microsoft.com/en-us/library/ms229650(v=VS.90).aspx

回答by AFract

The answer of Eric Lippert perfectly describes the situation.

Eric Lippert 的回答完美地描述了这种情况。

I just want to add a quick answer about a case which is usually not covered by help pages regarding this exception.

我只想添加一个有关此异常的帮助页面通常未涵盖的案例的快速答案。

I've created a quick & dirty test project for some open source stuff (Akka.Net, to name it) and I name the project itself "Akka".

我为一些开源的东西(Akka.Net,命名)创建了一个快速而肮脏的测试项目,我将项目本身命名为“Akka”。

It perfectly builds, but at startup it throws it type load exception regarding a class in Akka.dll.

它完美地构建,但在启动时它抛出它关于 Akka.dll 中的类的类型加载异常。

This is just because my executable (akka.exe) and the reference (akka.dll) have the same name. It took me a few minutes to figure this (I've began by things such as copy local, target platform, exact version... etc).

这只是因为我的可执行文件 (akka.exe) 和引用 (akka.dll) 具有相同的名称。我花了几分钟才弄清楚这一点(我从复制本地、目标平台、确切版本等开始)。

It's something very dumb but not forcibly the first thing which you will think (especially since I used nuget for dependancies), so I thought it could be interesting to share it : you will encounter TypeLoadException if your EXE and a dependancy have the same name.

这是一件非常愚蠢的事情,但不是您会想到的第一件事(尤其是因为我将 nuget 用于依赖项),所以我认为分享它可能很有趣:如果您的 EXE 和依赖项具有相同的名称,您将遇到 TypeLoadException。

回答by Hudson

This almost drove me crazy...

这几乎把我逼疯了......

I don't know how I managed this, but for some reason I had an old version of the DLL in GAC. Try looking for an old assembly there and remove it.

我不知道我是如何管理这个的,但出于某种原因,我在 GAC 中有一个旧版本的 DLL。尝试在那里寻找旧组件并将其删除。

Best of luck!

祝你好运!

回答by Mroczny Arturek

In my case problem was that I had two projects that used the same libraries in one solution. I've updated DLLs only in first project. So when I built solution, second project its override DLLs from first project(project build order).

就我而言,问题是我有两个项目在一个解决方案中使用了相同的库。我只在第一个项目中更新了 DLL。因此,当我构建解决方案时,第二个项目将覆盖第一个项目(项目构建顺序)中的 DLL。

Example:

Solution:

--MainProject

------MyDll v5.3.2.0

--Prototype

------MyDll v5.0.0.0

例子:

解决方案:

--主项目

------MyDll v5.3.2.0

- 原型

------MyDll v5.0.0.0

Problem gone after update DLLs in second project.

在第二个项目中更新 DLL 后问题消失了。

回答by makesha.laun

Make sure the names (namespaces, class names, etc) are what they are supposed to be. I got this error when I had to start over on my project and I copied the contents of my class from a template and failed to change the class name from "Template Class" to the correct name (it was supposed to match the project name).

确保名称(命名空间、类名等)是它们应有的样子。当我不得不重新开始我的项目时,我遇到了这个错误,我从模板中复制了我的类的内容,但未能将类名从“模板类”更改为正确的名称(它应该与项目名称匹配) .

回答by Eyad Husseini

the typeName string parameter must be a fully qualified type name

typeName 字符串参数必须是完全限定的类型名称

For instance : Activator.CreateInstance("assemblyFullName","namespace.typename")

例如:Activator.CreateInstance("assemblyFullName","namespace.typename")