AppLS
AppLS

Reputation: 116

.net standard 2 reference with enums break on .net 4.7.1 but work on .net 4.6.1

Start new project, pick .netcore2 console app. Change the target framework to .net 461. You do this by editing the .csproj file as shown:

<TargetFramework>net461</TargetFramework>

netcore has ran on the full framework for years. so no surprises. now add a new project: .net standard 2.0 class library. your .csproj on that library should now contain

<TargetFramework>netstandard2.0</TargetFramework>

reference this standard 2 assembly from your console app. Your .csproj file for the console app now reads:

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\LibStandard\LibStandard.csproj" />
  </ItemGroup>

create an enum on your .net standard 2 library

namespace LibStandard
{
    public class Class1
    {

    }

    public enum TestEnum
    {
        One, Two
    }

}

use said enum in your console app

class Program
{
    static void Main(string[] args)
    {
        TestEnum t = TestEnum.One;
        Console.WriteLine("Hello World!");
    }
}

works. cool. Now change your target framework on the console app to .net471. like so

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net471</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\LibStandard\LibStandard.csproj" />
  </ItemGroup>

and now you will get this error on build:

2>Program.cs(10,13,10,21): error CS0012: The type 'Enum' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
2>Program.cs(10,26,10,34): error CS0012: The type 'Enum' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
2>Program.cs(10,35,10,38): error CS0012: The type 'Enum' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
2>Done building project "ConsoleOne.csproj" -- FAILED.

I tried adding (via nuget) .netstandard 2.0.0 to the console app project, but that doesn't solve the problem.

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net471</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NETStandard.Library" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\LibStandard\LibStandard.csproj" />
  </ItemGroup>

If you haven't tried this before, you could always run .net standard libraries in 461 for 1.x standard. But the same doesn't work for .netstandard 2 and 471. You can also try adding a new console app (desktop app full netcore 471). Same result. Starting with a .netcore console app and then targeting the .netfx or starting without .net core gets the same error.

I'm stumped.

SAMPLE SOLUTION: SAMPLE

Upvotes: 1

Views: 2134

Answers (1)

AppLS
AppLS

Reputation: 116

Seems related to this per VS team https://github.com/Microsoft/msbuild/pull/2567

workaround seems to work: add _HasReferenceToSystemRuntime

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net471</TargetFramework>
    <_HasReferenceToSystemRuntime>true</_HasReferenceToSystemRuntime>
  </PropertyGroup>

seems Visual Studio still a bit confused dealing with .net standard

Upvotes: 2

Related Questions