【发布时间】:2017-07-11 22:00:38
【问题描述】:
重现步骤:
- 打开 Visual Studio 2017,新建类库项目
.NET 4.6.1。 - 使用 Nuget 包管理器添加对
Newtonsoft.Json的引用。 - 使用
VS2017成功构建项目。 - 打开命令行并从项目目录运行
dotnet build。
它给出了以下错误:
错误 CS0246:找不到类型或命名空间名称“Newtonsoft”(您是否缺少 using 指令或程序集引用?)
任何想法如何摆脱这个错误?
编辑: 运行 dotnet restore 之前:
运行 dotnet restore 后
用文件编辑: 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; }
}
}
【问题讨论】:
-
请提供minimal reproducible example,作为文本而不是屏幕截图。听起来你还没有运行
dotnet restore... -
@JonSkeet 嘿乔恩。实际上我已经尝试过这样做。请看编辑。关于最小的例子,我给了你repro。截图只是为了显示版本。
-
与其告诉我们您是如何做到的,不如向我们展示文件,以便我们可以复制/粘贴/构建。
-
我刚刚在Visual Studio中试了一下,还不错。
-
什么文件?你的意思是解决方案文件?