开发者

Compile Managed C++ Assembly Against Specific .NET Version (VS2008)

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 com开发者_如何学JAVApile 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):

  1. I create a C# project targeting .NET 3.5.
  2. I create an empty C++ project in the same solution.
  3. Add an empty .cpp file to the C++ project.
  4. 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
  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


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.


Your "fix" won't actually fix the problem.

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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜