C# Dotnet构建不需要';t与newcsproj和PackageReference合作

C# Dotnet构建不需要';t与newcsproj和PackageReference合作,c#,.net,C#,.net,复制步骤: 打开Visual Studio 2017,创建新的类库项目,.NET 4.6.1 使用Nuget软件包管理器添加对Newtonsoft.Json的引用 使用VS2017成功构建项目 打开命令行并从项目目录运行dotnet build 它给出了以下错误: 错误CS0246:找不到类型或命名空间名称“Newtonsoft”(是否缺少using指令或程序集引用?) dotnet版本: 有没有办法摆脱这个错误 编辑: 在运行dotnet还原之前: 运行dotnet还原后 使用文件编辑:

复制步骤:

  • 打开Visual Studio 2017,创建新的类库项目,
    .NET 4.6.1
  • 使用Nuget软件包管理器添加对
    Newtonsoft.Json
    的引用
  • 使用
    VS2017
    成功构建项目
  • 打开命令行并从项目目录运行
    dotnet build
  • 它给出了以下错误:

    错误CS0246:找不到类型或命名空间名称“Newtonsoft”(是否缺少using指令或程序集引用?)

    dotnet版本:

    有没有办法摆脱这个错误

    编辑: 在运行dotnet还原之前:

    运行dotnet还原后

    使用文件编辑: ClassLibrary1.csproj

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{42A41D81-0A26-4D79-935E-6002BFAD37EB}</ProjectGuid>
        <OutputType>Library</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>ClassLibrary1</RootNamespace>
        <AssemblyName>ClassLibrary1</AssemblyName>
        <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Debug\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Core" />
        <Reference Include="System.Xml.Linq" />
        <Reference Include="System.Data.DataSetExtensions" />
        <Reference Include="Microsoft.CSharp" />
        <Reference Include="System.Data" />
        <Reference Include="System.Net.Http" />
        <Reference Include="System.Xml" />
      </ItemGroup>
      <ItemGroup>
        <Compile Include="Class1.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
      </ItemGroup>
      <ItemGroup>
        <PackageReference Include="Newtonsoft.Json">
          <Version>9.0.1</Version>
        </PackageReference>
      </ItemGroup>
      <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    </Project>
    

    要在命令行上引用库,需要使用/r:compiler选项。 例如,运行
    csc/r:Path\u to\u your\u DLL/t:Path\u to\u your\u.cs\u文件

    根据,当您在VisualStudio中使用nuget时,VisualStudio似乎会自动包含所有库,但由于您试图使用命令行进行构建,因此您必须自己手动引用所有库。

    您使用的是“旧式”msbuild项目,它不能与
    dotnet
    CLI一起使用

    将整个项目文件替换为:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>net461</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
      </ItemGroup>
    
    </Project>
    
    
    net461
    

    现在,它将与
    dotnet
    CLI兼容,一切都应该很好。

    请提供一个文本而不是屏幕截图。听起来你好像还没有运行
    dotnet restore
    …@JonSkeet嘿,Jon。实际上我已经试过了。请看编辑。关于最小的例子,我给了你一个复制品。屏幕截图只是显示版本。与其告诉我们您是如何完成的,不如向我们显示文件,以便我们可以复制/粘贴/生成。我刚刚在Visual Studio中试用过,效果不错。什么文件?你是说解决方案文件?我通过nuget package manager引用它,为什么我要在命令行中手动执行它?@MistyK Updated answerPackageReference是新csproj的一部分,不是吗?当我创建类库(.NET Framework)项目时,Visual Studio为我创建了这个csproj。我已在Visual Studio设置中选择默认使用PackageReference。@MistyK:不,这肯定是csproj的旧样式,即使它现在支持PackageReference。如果你想使用.NETCoreSDK,你需要“类库(.NETCore)”,即使你把它设为目标net461(根据我的例子)。啊,这很有道理。所以创建.NETCore类库项目并不一定意味着它构建在.NETCore之上,因为我仍然可以选择.NETFramework。我很确定这只是ASP.NET核心项目的情况。很酷,谢谢!
    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>net461</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
      </ItemGroup>
    
    </Project>