gpt4 book ai didi

c# - DataContractJsonSerializer 不可用

转载 作者:行者123 更新时间:2023-11-30 17:32:02 25 4
gpt4 key购买 nike

环境:Windows 10 Fall Creators Update (1709) 上的 Visual Studio 2017

目标:使用 DataContractJsonSerializer 序列化一个 JSON 对象

问题:我无法解析 DataContractJsonSerializer。查看 y 项目引用,我尝试将 System.Runtime.Serializtion (v4.0.30319) 添加到我的引用中,但没有成功。是否需要额外的引用/包来访问 DataContractJsonSerializer。

替代方案:是的,我知道我可以使用 NewtonSoft.Json 并且可能会使用,但在向我的项目添加额外的依赖项之前想要一个决定性的答案。

研究:我尝试在 SO 和 Google 上寻找,DataContractJsonSerializer 仍然被高度引用,但找不到对我来说至少很明显可能出错的东西。

项目文件

 <?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>{53E26A85-0C38-4B64-9C45-7FF41EBF22F5}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ConsoleApp1</RootNamespace>
<AssemblyName>ConsoleApp1</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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.Runtime.Serialization" />
<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="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

输出:

1>------ Build started: Project: ConsoleApp1, Configuration: Debug Any CPU ------
1>C:\Users\amarne\source\repos\ConsoleApp1\ConsoleApp1\Program.cs(24,13,24,39): error CS0246: The type or namespace name 'DataContractJsonSerializer' could not be found (are you missing a using directive or an assembly reference?)
1>C:\Users\amarne\source\repos\ConsoleApp1\ConsoleApp1\Program.cs(24,50,24,76): error CS0246: The type or namespace name 'DataContractJsonSerializer' could not be found (are you missing a using directive or an assembly reference?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

感谢 Yaakov,我知道发生了什么。对于第一次使用 DataContractJsonSerializer 的用户,请记住在执行此操作后手动添加对 System.Runtime.Serialization 的引用您应该能够解决“使用 System.Runtime.Serialization.Json;”

在我的例子中,我猜想在添加引用后我只是看到 VS 不满意然后没有手动构建它。所以我在正确的轨道上只是没有给它足够的时间:)

关于c# - DataContractJsonSerializer 不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47046378/

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