【发布时间】:2018-02-20 03:26:07
【问题描述】:
我有一个命令行 .NET 应用程序,当构建为 .NET Core 应用程序时,它应该只针对移植的 .NET Core 组件进行编译,但以这种方式构建时还可以引用旧版 .NET 框架模块。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
<ProjectGuid>{48B4C337-CD13-4826-B6A1-DA97CE71511B}</ProjectGuid>
<RootNamespace>Microsoft.Pc</RootNamespace>
<AssemblyName>Pc</AssemblyName>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<OutputPath>$(PSdkFolder)\Binaries</OutputPath>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
<ProjectReference Include="..\CompilerCore\CompilerCore.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
<ProjectReference Include="..\CompilerCore\CompilerCore.csproj" />
<ProjectReference Include="..\LegacyCompiler\LegacyCompiler.csproj" />
<ProjectReference Include="..\CompilerService\CompilerService.csproj" />
</ItemGroup>
</Project>
尽管如此,我还是得到了这些错误
"D:\Source\Repos\P\P.sln" (default target) (1) ->
"D:\Source\Repos\P\Src\Pc\CommandLine\CommandLine.csproj.metaproj" (default target) (20) ->
"D:\Source\Repos\P\Src\Pc\CommandLine\CommandLine.csproj" (default target) (21) ->
"D:\Source\Repos\P\Src\Pc\CommandLine\CommandLine.csproj" (Build target) (21:3) ->
(_GetProjectReferenceTargetFrameworkProperties target) ->
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\Microsoft.Common.CurrentVersion.targets(1601,5): error : Project 'D:\Source\Repos\P\Src\Pc\LegacyCompiler\LegacyCompiler.csproj' targets 'net461'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v2.0'. [D:\Source\Repos\P\Src\Pc\CommandLine\CommandLine.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\Microsoft.Common.CurrentVersion.targets(1601,5): error : Project 'D:\Source\Repos\P\Src\Pc\CompilerService\CompilerService.csproj' targets 'net461'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v2.0'. [D:\Source\Repos\P\Src\Pc\CommandLine\CommandLine.csproj]
为什么在这种情况下应该排除依赖项时,它会抱怨 LegacyCompiler.csproj 以“net461”为目标,而 CommandLine 以“.NETCoreApp,Version=v2.0”为目标?
更新:这些是其他项目的 .csproj 文件。由于这个项目是开源的,所以我发布了文件的链接:
【问题讨论】:
-
我只能说这是正确的做法,而且它一直(并且一直)为我工作。
-
@Evk -- 这也是我能找到的所有文档所建议的。我将为其他两个项目添加 .csproj...
-
如果您无法解决此问题 - 您可以在某处发布最小可重现示例(带有空项目的 VS 解决方案),以便人们查看。
-
这个项目是开源的。你可以从
ssa分支在这里克隆github.com/p-org/P/tree/ssa -
我应该提到我正在使用
msbuild进行编译。也许我缺少一面旗帜?我可以使用dotnet build命令构建.NET 4.6.1 目标吗?