title: Unable to use a CLR type from CLI class library in a C# project in Visual Studio 2012 targeting .NET 3.5 and compiling with Windows7.1SDK toolset
body: <p>I have a VS2012 solution with a C++/CLI Class Library project targeting .NET 3.5 and using the the Windows7.1SDK as Platform Toolset, which according to <a href="https://msdn.microsoft.com/en-us/library/ff770576%28v=vs.110%29.aspx" rel="nofollow noreferrer">MSDN</a> is the correct way to target previous versions of the framework without installing the corresponding version of the toolset (i.e. Visual Studio 2008):</p>
  
  <blockquote>
    <p>You can use the <strong>Windows7.1SDK</strong> platform toolset to target the .NET
    Framework 2.0, 3.0, 3.5, and 4, and the x86, Itanium, and x64
    platforms.</p>
  </blockquote>
  
  <p>The CLI/C++ project has Common Language Runtime Support (/clr) enabled.</p>
  
  <p>I would like to use a class declared in the C++ project in C#, so I create a C# project targeting .NET 3.5 and add a reference to the CLI project. However, when building the C# that uses the type declared in the C++ project, I get: <em>The type or namespace name 'Class1' could not be found (are you missing a using directive or an assembly reference?)</em></p>
  
  <p>The strange thing is that the IDE does seem to be able to find the type (green color) and IntelliSense shows the methods available for the type.</p>
  
  <p>Solution Structure</p>
  
  <pre><code>Solution Test1
  |
  | ClassLibrary1 (C++)
  |    Class1.h
  |    Class1.cpp
  |
  | Test1 (C#)
  |    TestClass1.cs
  </code></pre>
  
  <p>ClassLibrary1.h</p>
  
  <pre><code>public ref class Class1
  {
  public:
      Class1(unsigned short int initValue);
      ~Class1(void);
  
      void Init(unsigned short int init);
      void Input(unsigned char data);
      unsigned short int Getalue(void);
  
  private:
      unsigned short int _value;
  };
  </code></pre>
  
  <p>TestClass1.cs</p>
  
  <pre><code>static void Main(string[] args)
  {
      var t = new Class1(0xFFFF); //also tried with global::Class1, no cigar
  }
  </code></pre>
  
  <p>Even more strangely, VS identifies the reference to the C++/CLI project as targeting v4.0.30319.</p>
  
  <p><a href="https://i.stack.imgur.com/sJueN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sJueN.png" alt=""></a></p>
  
  <p>It also gives warnings like </p>
  
  <blockquote>
    <p>Warning   5   The primary reference "D:\Test1\Debug\ClassLibrary1.dll"
    could not be resolved because it has an indirect dependency on the
    .NET Framework assembly "System.Xml, Version=4.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089" which has a higher version "4.0.0.0"
    than the version "2.0.0.0" in the current target framework.   Test1</p>
  </blockquote>
  
  <p>I can't figure out where it's getting the idea that this has anything to do with .NET 4. I found ClassLibrary1.dll.metagen in the Debug folder, which is the only place I can think of it would be getting these references to v4 of the assemblies:</p>
  
  <pre><code>ImageRuntimeVersion: v4.0.30319
  Assembly ClassLibrary1, Version=1.0.*, Culture=Invariant Language (Invariant Country): 
      hash=SHA1, flags=PublicKey
  Assembly mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089: 
      hash=None, flags=None
  Assembly System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089: 
      hash=None, flags=None
  Assembly System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089: 
      hash=None, flags=None
  Assembly System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089: 
      hash=None, flags=None
  Assembly Microsoft.VisualC, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a: 
      hash=None, flags=None
  Assembly mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089: 
      hash=None, flags=None
  Assembly System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089: 
      hash=None, flags=None
  Assembly Microsoft.VisualC, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a: 
      hash=None, flags=None
  Class Class1: AutoLayout, AnsiClass, Class, Public, BeforeFieldInit
      Void .ctor(UInt16): PrivateScope, Public, HideBySig, SpecialName, RTSpecialName
    Interfaces:
      System.IDisposable
    Methods:
      Init(UInt16): PrivateScope, Public, HideBySig
      Input(Byte): PrivateScope, Public, HideBySig
      GetValue(): PrivateScope, Public, HideBySig
      Dispose(): PrivateScope, Public, Final, Virtual, HideBySig
  </code></pre>
  
  <p>I've tried editing the file with no success. I've also tried switching the toolset to Visual Studio 2012 (v110) in the C++ project without success. Any help would be appreciated.</p>
  
  <p>It seems like someone already had the <a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/7f85bac4-275e-491a-be81-242bdb0e7cad/managed-c-class-the-type-or-namespace-name-could-not-be-found?forum=vcgeneral" rel="nofollow noreferrer">exact same issue</a> over on the VS Forums site, but eventually gave up trying to resolve it.</p>
  
  <p>Related questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/24775363/how-to-build-with-v90-platform-toolset-in-vs2012-without-vs2008-using-windows-s">How to build with v90 platform toolset in VS2012 without VS2008, using Windows SDK?</a></p>
  
  <p>ClassLibrary1.vcxproj</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    &lt;ItemGroup Label="ProjectConfigurations"&gt;
      &lt;ProjectConfiguration Include="Debug|Win32"&gt;
        &lt;Configuration&gt;Debug&lt;/Configuration&gt;
        &lt;Platform&gt;Win32&lt;/Platform&gt;
      &lt;/ProjectConfiguration&gt;
      &lt;ProjectConfiguration Include="Release|Win32"&gt;
        &lt;Configuration&gt;Release&lt;/Configuration&gt;
        &lt;Platform&gt;Win32&lt;/Platform&gt;
      &lt;/ProjectConfiguration&gt;
    &lt;/ItemGroup&gt;
    &lt;PropertyGroup Label="Globals"&gt;
      &lt;ProjectGuid&gt;{B2C66274-DF38-473F-B759-C7E2E7A1E8A3}&lt;/ProjectGuid&gt;
      &lt;TargetFrameworkVersion&gt;v3.5&lt;/TargetFrameworkVersion&gt;
      &lt;Keyword&gt;ManagedCProj&lt;/Keyword&gt;
      &lt;RootNamespace&gt;ClassLibrary1&lt;/RootNamespace&gt;
    &lt;/PropertyGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"&gt;
      &lt;ConfigurationType&gt;DynamicLibrary&lt;/ConfigurationType&gt;
      &lt;UseDebugLibraries&gt;true&lt;/UseDebugLibraries&gt;
      &lt;PlatformToolset&gt;Windows7.1SDK&lt;/PlatformToolset&gt;
      &lt;CLRSupport&gt;true&lt;/CLRSupport&gt;
      &lt;CharacterSet&gt;Unicode&lt;/CharacterSet&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"&gt;
      &lt;ConfigurationType&gt;DynamicLibrary&lt;/ConfigurationType&gt;
      &lt;UseDebugLibraries&gt;false&lt;/UseDebugLibraries&gt;
      &lt;PlatformToolset&gt;Windows7.1SDK&lt;/PlatformToolset&gt;
      &lt;CLRSupport&gt;true&lt;/CLRSupport&gt;
      &lt;CharacterSet&gt;Unicode&lt;/CharacterSet&gt;
    &lt;/PropertyGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /&gt;
    &lt;ImportGroup Label="ExtensionSettings"&gt;
    &lt;/ImportGroup&gt;
    &lt;ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;
      &lt;Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /&gt;
    &lt;/ImportGroup&gt;
    &lt;ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;
      &lt;Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /&gt;
    &lt;/ImportGroup&gt;
    &lt;PropertyGroup Label="UserMacros" /&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;
      &lt;LinkIncremental&gt;true&lt;/LinkIncremental&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;
      &lt;LinkIncremental&gt;false&lt;/LinkIncremental&gt;
    &lt;/PropertyGroup&gt;
    &lt;ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;
      &lt;ClCompile&gt;
        &lt;WarningLevel&gt;Level3&lt;/WarningLevel&gt;
        &lt;Optimization&gt;Disabled&lt;/Optimization&gt;
        &lt;PreprocessorDefinitions&gt;WIN32;_DEBUG;%(PreprocessorDefinitions)&lt;/PreprocessorDefinitions&gt;
        &lt;PrecompiledHeader&gt;Use&lt;/PrecompiledHeader&gt;
      &lt;/ClCompile&gt;
      &lt;Link&gt;
        &lt;GenerateDebugInformation&gt;true&lt;/GenerateDebugInformation&gt;
        &lt;AdditionalDependencies /&gt;
      &lt;/Link&gt;
    &lt;/ItemDefinitionGroup&gt;
    &lt;ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;
      &lt;ClCompile&gt;
        &lt;WarningLevel&gt;Level3&lt;/WarningLevel&gt;
        &lt;PreprocessorDefinitions&gt;WIN32;NDEBUG;%(PreprocessorDefinitions)&lt;/PreprocessorDefinitions&gt;
        &lt;PrecompiledHeader&gt;Use&lt;/PrecompiledHeader&gt;
      &lt;/ClCompile&gt;
      &lt;Link&gt;
        &lt;GenerateDebugInformation&gt;true&lt;/GenerateDebugInformation&gt;
        &lt;AdditionalDependencies /&gt;
      &lt;/Link&gt;
    &lt;/ItemDefinitionGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ClInclude Include="Class1.h" /&gt;
      &lt;ClInclude Include="resource.h" /&gt;
      &lt;ClInclude Include="Stdafx.h" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ClCompile Include="AssemblyInfo.cpp" /&gt;
      &lt;ClCompile Include="Class1.cpp" /&gt;
      &lt;ClCompile Include="Stdafx.cpp"&gt;
        &lt;PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;Create&lt;/PrecompiledHeader&gt;
        &lt;PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;Create&lt;/PrecompiledHeader&gt;
      &lt;/ClCompile&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Text Include="ReadMe.txt" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ResourceCompile Include="app.rc" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Image Include="app.ico" /&gt;
    &lt;/ItemGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /&gt;
    &lt;ImportGroup Label="ExtensionTargets"&gt;
    &lt;/ImportGroup&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <p>Test1.csproj</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    &lt;Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /&gt;
    &lt;PropertyGroup&gt;
      &lt;Configuration Condition=" '$(Configuration)' == '' "&gt;Debug&lt;/Configuration&gt;
      &lt;Platform Condition=" '$(Platform)' == '' "&gt;AnyCPU&lt;/Platform&gt;
      &lt;ProjectGuid&gt;{160BE075-BAF3-48CE-8CB3-E607D5F91A50}&lt;/ProjectGuid&gt;
      &lt;OutputType&gt;Library&lt;/OutputType&gt;
      &lt;AppDesignerFolder&gt;Properties&lt;/AppDesignerFolder&gt;
      &lt;RootNamespace&gt;Test1&lt;/RootNamespace&gt;
      &lt;AssemblyName&gt;Test1&lt;/AssemblyName&gt;
      &lt;TargetFrameworkVersion&gt;v3.5&lt;/TargetFrameworkVersion&gt;
      &lt;FileAlignment&gt;512&lt;/FileAlignment&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "&gt;
      &lt;DebugSymbols&gt;true&lt;/DebugSymbols&gt;
      &lt;DebugType&gt;full&lt;/DebugType&gt;
      &lt;Optimize&gt;false&lt;/Optimize&gt;
      &lt;OutputPath&gt;bin\Debug\&lt;/OutputPath&gt;
      &lt;DefineConstants&gt;DEBUG;TRACE&lt;/DefineConstants&gt;
      &lt;ErrorReport&gt;prompt&lt;/ErrorReport&gt;
      &lt;WarningLevel&gt;4&lt;/WarningLevel&gt;
      &lt;PlatformTarget&gt;x86&lt;/PlatformTarget&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "&gt;
      &lt;DebugType&gt;pdbonly&lt;/DebugType&gt;
      &lt;Optimize&gt;true&lt;/Optimize&gt;
      &lt;OutputPath&gt;bin\Release\&lt;/OutputPath&gt;
      &lt;DefineConstants&gt;TRACE&lt;/DefineConstants&gt;
      &lt;ErrorReport&gt;prompt&lt;/ErrorReport&gt;
      &lt;WarningLevel&gt;4&lt;/WarningLevel&gt;
    &lt;/PropertyGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Reference Include="System" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Compile Include="TestClass1.cs" /&gt;
      &lt;Compile Include="Properties\AssemblyInfo.cs" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ProjectReference Include="..\ClassLibrary1\ClassLibrary1.vcxproj"&gt;
        &lt;Project&gt;{b2c66274-df38-473f-b759-c7e2e7a1e8a3}&lt;/Project&gt;
        &lt;Name&gt;ClassLibrary1&lt;/Name&gt;
      &lt;/ProjectReference&gt;
    &lt;/ItemGroup&gt;
    &lt;Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /&gt;
    &lt;!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
         Other similar extension points exist, see Microsoft.Common.targets.
    &lt;Target Name="BeforeBuild"&gt;
    &lt;/Target&gt;
    &lt;Target Name="AfterBuild"&gt;
    &lt;/Target&gt;
    --&gt;
  &lt;/Project&gt;
  </code></pre>