编译托管 C++针对特定 .NET 版本的汇编 (VS2008)
我的托管 C++ 程序集遇到了一个奇怪的问题。尽管我将 C# 项目和托管 C++ (DLL) 项目设置为目标 .NET 3.5,但当我编译托管程序集时,其“运行时版本”为 2.0。
我尝试在 .NET 3.5 项目和 .NET 4.0 版本的 CSScript 中使用相同的程序集,并且 CSScript 拒绝与 .NET 2.0 程序集一起运行(“运行时异常:混合模式程序集是针对版本 'v2.0”构建的)。 0.50727' 的运行时,如果没有附加配置信息,则无法在 4.0 运行时加载。”)。
我设法将问题定位为以下内容(所有工作均在 VS 2008 中完成):
- 我创建了一个针对 .NET 3.5 的 C# 项目。
- 我在同一解决方案中创建了一个空的 C++ 项目。
- 将空的 .cpp 文件添加到 C++ 项目中。
- 更改 C++ 项目属性:配置类型 = 动态库 (.dll)、公共语言运行时支持 = 公共语言运行时支持 (/clr),并确保公共属性的“目标框架”设置为 3.5
- 编译 C++ 项目并将其添加为对 C# 项目的引用。
当您查看 C# 项目中引用的属性时,您将看到“运行时版本”是 v2.0.50727。我的问题是,到底是什么?
为什么运行时版本是2.0?我将所有内容设置为 3.5 ...如何强制托管 C++ 项目针对 .NET 3.5 进行编译?
谢谢, 马雷克
I am running into a weird issue with my managed C++ assembly. Even though I have the C# project and the managed C++ (DLL) project set to target .NET 3.5, when I compile the managed assembly its "Runtime Version" is 2.0.
I am trying to use the same assembly across a .NET 3.5 project and a .NET 4.0 version of a CSScript and the CSScript refuses to run with a .NET 2.0 assembly ("Runtime Exception: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.").
I managed to localize the problem to the following (all work done in VS 2008):
- I create a C# project targeting .NET 3.5.
- I create an empty C++ project in the same solution.
- Add an empty .cpp file to the C++ project.
- Change the C++ project properties: Configuration Type = Dynamic Library (.dll), Common Language Runtime support = Common Language Runtime Support (/clr), and make sure that Common Properties' "Targeted Framework" is set to 3.5
- Compile the C++ project and add it as a reference to the C# project.
When you look at the properties of the reference in the C# project you will see the "Runtime Version" is v2.0.50727. My question is, what the hell?
Why is the runtime version 2.0? I set everything to 3.5 ... How can I force the managed C++ project to compile against .NET 3.5?
Thanks,
Marek
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
运行时版本是指虚拟机的版本,而不是.NET类库的版本。 .NET 3.5 与 .NET 2.0 在同一虚拟机上运行,这就是您的参考显示 v2.0.50727 的原因。如果您想查看 C++ 项目是否确实针对 3.5 版本的框架,您应该检查该项目中的各个库引用。
The runtime version refers to the version of the virtual machine, not the .NET class libraries. .NET 3.5 runs on the same virtual machine as .NET 2.0, that's why your reference shows v2.0.50727. If you want to see if the C++ project is actually targeting the 3.5 version of the framework, you should check the individual library references in that project.
你的“修复”并不能真正解决问题。
您需要使用 4.0 运行时来避免这种情况,甚至尝试一些神话般的“没有意义” 3.5英寸版本。为此,您需要使用 VC++ 2010 编译器进行编译。由于您需要在 .NET 3.5 应用程序中使用它,所以这是不可能的。
尝试查看“附加配置信息”。请参阅此问题以及此问题一个。
Your "fix" won't actually fix the problem.
You need to use the 4.0 runtime to avoid this, no point in even trying for some mythical "3.5" version. For that, you need to compile using the VC++ 2010 compiler. Since you need to use it in a .NET 3.5 application, that's a non-starter.
Try looking into that "additional configuration information". See this question and also this one.