0

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):

  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

marco1475
  • 55
  • 1
  • 6
  • possible duplicate of [What 'additional configuration' is necessary to reference a .NET 2.0 mixed mode assembly in a .NET 4.0 project?](http://stackoverflow.com/questions/2455654/what-additional-configuration-is-necessary-to-reference-a-net-2-0-mixed-mode-a) – Ben Voigt Sep 16 '11 at 20:07

2 Answers2

1

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.

Peter Ruderman
  • 12,241
  • 1
  • 36
  • 58
0

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.

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thanks for your answer. I thought that I can use assemblies built in 3.5 in 4.0, just not 2.0? If even assemblies compiled against 3.5 won't work, then yes, my attempt at sharing the same DLL across the two projects is moot. I just need clarification, because I think I read somewhere that .NET 3.5 is compatible (as in "can be run by") with 4.0, but anything less than 3.5 isn't ... – marco1475 Sep 16 '11 at 23:00
  • @marco: You're missing a piece that makes it work. Read the questions I linked to. 2.0 and 3.5 both work the same, which is to say, not until you change the configuration. – Ben Voigt Sep 17 '11 at 02:05
  • The important information was that in .NET 4.0 the app.config file is required for all previous versions of .NET, not just 2.0 which what I was led to believe (all the examples deal with 2.0, none with 3.5). Unfortunately, since I cannot use the app.config file in CSScript (a scripting language that compiles .NET on the fly before executing it) I am left with having two versions of the assembly, one for the 3.5 project and one for the 4.0 script. Thanks again for your answers. – marco1475 Sep 17 '11 at 12:32