ReflectionTypeLoadException 从 .Net 4.0 应用程序加载 .Net 4.0 程序集,而 2.0 可以工作
我有一个 .NET 4.0 32 位 (x86) 桌面应用程序,其中包含以下文件,其中之一是从用托管 C++ 编写的包装器库调用的旧版非托管库。
- Application.exe(C#、.net 4)
- Library.dll(C#、.net 4)
- UnmanagementWrapper.dll(托管 c++)
- Unmanagement.dll (c)
文件 1 和 2 由 VS 2010 解决方案构建。这些的 corflags 如下所示:
Version : v4.0.30319
CLR Header: 2.5
PE : PE32
CorFlags : 3
ILONLY : 1
32BIT : 1
Signed : 0
非托管包装器库最初是使用 VS 2008 构建的,如下所示:
Version : v2.0.50727
CLR Header: 2.5
PE : PE32
CorFlags : 16
ILONLY : 0
32BIT : 0
Signed : 0
在此配置中,它可以正常工作。然后,我将非托管包装项目迁移到 VS 2010 中构建,默认情况下将 .NET 4 作为目标:
Version : v4.0.30319
CLR Header: 2.5
PE : PE32
CorFlags : 16
ILONLY : 0
32BIT : 0
Signed : 0
在 Library.dll 内的一段代码中,我使用 Assembly.GetExecutingAssembly().GetTypes()
查找实现库内某个接口的类型。由于该库引用了非托管包装器,因此此时将加载它。 在多台机器(所有 Win 7 Pro x64 机器)上运行它,我得到不同的结果: 在一些计算机上,它会抛出 ReflectionTypeLoadException
,抱怨无法加载 UnmanagementWrapper.dll 或其依赖项之一。
它在其他机器上完美运行。无论包装器库是 2.0 还是 4.0,它都可以工作的机器与仅当包装器是 2.0 时它才工作的机器之间有什么区别?到目前为止我唯一的线索是 安装 VS 2010 的计算机似乎可以正常工作,而仅安装 .NET (4.0.30319) 的计算机则会出现异常。那应该告诉我什么?
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++.
- Application.exe (C#, .net 4)
- Library.dll (C#, .net 4)
- UnmanagedWrapper.dll (managed c++)
- Unmanaged.dll (c)
The files 1 and 2 are built by a VS 2010 solution. 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:
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:
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. 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.
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? 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. What should that be telling me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果应用程序在安装了 Visual Studio 的地方无法运行,有时解决方案是需要安装 Visual Studio Runtime。尝试安装 Microsoft Visual C++ 2008 Redistributable Package (x86)< /a> 或 Microsoft Visual C++ 2010可再发行组件包 (x86),如果它有效,请确保它是您的部署策略的一部分。
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. 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.