4

I have the exact same issue as the post "C++/CLI DLL namespace not found in MSVS C# Project (successfully reproducable)", but didn't make the mistake that resolved things for the posting user.

I have a C++/CLI project that I've used in VS2005 for years. It contains some native code, and some CLR wrappers (ref class) to expose the native functionality to other managed (C#) projects. I recently converted this project to 2010 (.vcxproj), got it compiling OK, got it referenced from my C# project, but compiling the C# project gives error CS0234: The type or namespace name 'MySecondLevelNamespace' does not exist in the namespace 'MyTopLevelNamespace' (are you missing an assembly reference?).

I fear the conversion, although compiling, isn't exposing the ref classes to the managed callers. Both the CLI and C# projects are configured to target 2.0 runtime. What am I missing here?

Again, this code (both the CLI project, and the C# project calling it) all compiles without issue in VS2005. Thanks for your help!

Community
  • 1
  • 1
MECreegan
  • 41
  • 1

2 Answers2

3

Both the CLI and C# projects are configured to target 2.0 runtime.

That's probably where your problem lies. The VC2010 compiler can only target the .NET 4 runtime. The only way to build for .NET 2.0 is to configure Visual Studio 2010 to run the VC2005 compiler.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • So, when I open the common properties page of the CLI project and it says "Targeted Framework: .NetFramework,Version=v2.0" that's not meaningful? If you're saying that it's compiling the CLI project to 4.0 despite the project configuration, why would that cause the public ref classes not to be found? Also, if it's compiling to 4.0, I think VS would complain when adding a reference of a 4.0 project to a 2.0 project. Also, if I reference the built assembly directly (as opposed to referencing the project) the properties state the runtime is 2.0.50727. So, I'm pretty sure it's compiling to 2.0 – MECreegan May 12 '11 at 15:09
  • FWIW, VC++ 2008 can also target the 2.0 runtime. – ildjarn May 12 '11 at 18:00
  • @MECreegan : Target Framework is only useful in VC++ 2010 when set in conjunction with Platform Toolset; since VC++ 2010 cannot target a .NET runtime other than 4.0, an alternate toolset oriented around VC++ 2008 or VC++ 2005 must be used. This is documented [here](http://msdn.microsoft.com/en-us/library/ff770576.aspx). – ildjarn May 12 '11 at 18:04
  • I followed this documentation earlier. I'm still at a loss as to why when I build this project in VS 2010 with the Target Platform as 2.0 and the toolset as v9, the output assembly doesn't expose the CLR classes. Besides, even if I've done something wrong and VS 2010 is outputting the assembly targeting 4.0, I should be able to call the CLR classes from another 4.0 assembly, but I can't. It's as if VC++ 2010 just doesn't produce the right metadata. – MECreegan May 12 '11 at 21:14
  • @MECreegan: Wait, you said nothing about toolchain settings in your entire question. If you've selected v9, you're using VC2008 and not VC2010. – Ben Voigt May 13 '11 at 00:26
0

The simple solution is don't upgrade the C++/CLI solution, at least until you have a reason to change the target framework, if you are making tons of changes to the solution itself.

You shoudl think about migrating to the .NET 4.0 Framework should also solve your problems.

This answer is based on Ben's response.

Security Hound
  • 2,577
  • 3
  • 25
  • 42
  • Is it possible to have this project (vcproj) in a VS 2010 solution without converting it? When you suggest migrating to 4.0, are you saying if my calling C# project were targeting 4.0 I'd have better luck? Thanks. – MECreegan May 12 '11 at 15:20