.net标准2参考,enums在.net 4.7.1上中断,但在.net 4.6.1上工作



启动新项目,pick.netcore2控制台应用程序。将目标框架更改为.net 461。您可以编辑.csproj文件,如下所示:

<TargetFramework>net461</TargetFramework>

netcore多年来一直在完整的框架上运行。所以这并不奇怪。现在添加一个新项目:.net标准2.0类库。该库上的.csproj现在应该包含

<TargetFramework>netstandard2.0</TargetFramework>

从控制台应用程序中引用此标准2程序集。控制台应用程序的.csproj文件现在显示为:

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..LibStandardLibStandard.csproj" />
</ItemGroup>

在.net标准2库上创建一个枚举

namespace LibStandard
{
public class Class1
{
}
public enum TestEnum
{
One, Two
}
}

在控制台应用中使用所述枚举

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

工作。凉的现在将控制台应用程序上的目标框架更改为.net471。像这样

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net471</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..LibStandardLibStandard.csproj" />
</ItemGroup>

现在你会在构建时得到这个错误:

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.

我尝试(通过nuget)将.netstandard2.0.0添加到控制台应用程序项目中,但这并不能解决问题。

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net471</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NETStandard.Library" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..LibStandardLibStandard.csproj" />
</ItemGroup>

如果您以前没有尝试过,那么您可以在461中为1.x标准运行.net标准库。但这对.netstandard2和471不起作用。您也可以尝试添加一个新的控制台应用程序(桌面应用程序完整的netcore 471)。同样的结果。从.netcore控制台应用程序开始,然后以.netfx为目标,或者从没有.net core的情况下开始,都会出现同样的错误。

我被难住了。

样品溶液:样品

每个VS团队似乎都与此相关https://github.com/Microsoft/msbuild/pull/2567

解决方法似乎有效:添加_HasReferenceToSystemRuntime

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

Visual Studio在处理.net标准时似乎仍然有点困惑

最新更新