简体   繁体   English

ReflectionTypeLoadException从2.0版本的.Net 4.0应用程序中加载.Net 4.0的程序集

[英]ReflectionTypeLoadException loading .Net 4.0 Assembly from .Net 4.0 application, while 2.0 works

I have a .NET 4.0 32 bit (x86) desktop application with the following files, one of which is a legacy unmanaged library called from a wrapper lib written in managed c++. 我有一个带有以下文件的.NET 4.0 32位(x86)桌面应用程序,其中一个是从非托管库中调用的传统非托管库,该库是用托管c ++编写的包装库调用的。

  1. Application.exe (C#, .net 4) Application.exe(C#、. net 4)
  2. Library.dll (C#, .net 4) Library.dll(C#、. net 4)
  3. UnmanagedWrapper.dll (managed c++) UnmanagedWrapper.dll(托管C ++)
  4. Unmanaged.dll (c) Unmanaged.dll(c)

The files 1 and 2 are built by a VS 2010 solution. 文件1和2是由VS 2010解决方案构建的。 The corflags for these look as follows: 这些的标志如下所示:

Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32
CorFlags  : 3
ILONLY    : 1
32BIT     : 1
Signed    : 0

The unmanaged wrapper library was originally built with VS 2008, like this: 非托管包装器库最初是使用VS 2008构建的,如下所示:

Version   : v2.0.50727
CLR Header: 2.5
PE        : PE32
CorFlags  : 16
ILONLY    : 0
32BIT     : 0
Signed    : 0

In this configuration, it worked without problems. 在这种配置下,它可以正常工作。 Then I migrated the unmanaged wrapper project to be built in VS 2010 which gives .NET 4 as target by default: 然后,我迁移了要在VS 2010中构建的非托管包装项目,该项目默认将.NET 4作为目标:

Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32
CorFlags  : 16
ILONLY    : 0
32BIT     : 0
Signed    : 0

In a section of code inside Library.dll I use Assembly.GetExecutingAssembly().GetTypes() to find types implementing a certain interface inside the Library. 在Library.dll中的一段代码中,我使用Assembly.GetExecutingAssembly().GetTypes()查找在Library内部实现某个接口的类型。 Since the library references the unmanaged wrapper, it will be loaded at this point. 由于该库引用了非托管包装,因此将在此时加载它。 Running this on a number of machines (All Win 7 Pro x64 machines) I get varying results: On a few machines it throws a ReflectionTypeLoadException complaining that UnmanagedWrapper.dll or one of its dependencies can't be loaded. 在多台计算机(所有Win 7 Pro x64计算机)上运行此命令,得到的结果各不相同:在几台计算机上,它抛出ReflectionTypeLoadException抱怨无法加载UnmanagedWrapper.dll或其依赖项之一。

It works perfectly on other machines. 它可以在其他计算机上完美运行。 What could be the difference between the machines where it works regardless of whether the wrapper lib is 2.0 or 4.0, and those machines where it only works if the wrapper is 2.0? 无论包装器lib是2.0还是4.0,它都可以工作的机器与仅包装器是2.0的机器之间有什么区别? The only clue I have so far is that machines with VS 2010 seem to work, whereas machines with only a .NET (4.0.30319) install will see the exception. 到目前为止,我唯一的线索是,使用VS 2010的计算机似乎可以正常工作,而仅安装.NET(4.0.30319)的计算机将看到此异常。 What should that be telling me? 那应该告诉我什么?

In such cases where the applications doesn't work where Visual Studio is installed, occasionally the solution is that you need to install Visual Studio Runtime. 在安装Visual Studio的情况下应用程序无法正常工作的情况下,偶尔的解决方案是您需要安装Visual Studio Runtime。 Try installing either Microsoft Visual C++ 2008 Redistributable Package (x86) or Microsoft Visual C++ 2010 Redistributable Package (x86) and if it works make sure its part of your deployment strategy. 尝试安装Microsoft Visual C ++ 2008可再发行组件包(x86)Microsoft Visual C ++ 2010可再发行组件包(x86) ,如果可以,请确保将其作为部署策略的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM