gpt4 book ai didi

c# - Dotnet 构建不适用于 newcsproj 和 PackageReference

转载 作者:行者123 更新时间:2023-12-03 19:38:11 28 4
gpt4 key购买 nike

重现步骤:

  • 打开Visual Studio 2017,新建类库项目,.NET 4.6.1 .
  • 添加对 Newtonsoft.Json 的引用使用 Nuget 包管理器。
  • 使用 VS2017 成功构建项目.
  • 打开命令行并运行 dotnet build从项目目录。

  • 它给出了以下错误:

    error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)



    网络版:
    enter image description here

    任何想法如何摆脱这个错误?

    编辑:
    在运行 dotnet restore 之前:

    enter image description here

    运行 dotnet restore 后

    enter image description here

    使用文件编辑:
    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>

    Class1.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json;

    namespace ClassLibrary1
    {
    public class Class1
    {

    [JsonProperty]
    public string asd { get; set; }
    }
    }

    最佳答案

    您正在使用“旧式”msbuild 项目,该项目不适用于 dotnet命令行界面。

    将整个项目文件替换为:

    <Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
    </ItemGroup>

    </Project>

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

    关于c# - Dotnet 构建不适用于 newcsproj 和 PackageReference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45036260/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com