c# - 可移植类库在 .NET 4.6 中没有 DataContract 或序列化函数

标签 c# visual-studio-2015 datacontract .net-4.6

刚刚在全新的Windows 10上拿出了全新的Visual Studio 2015。我尝试创建一个简单的可移植类库(PCL),并尝试添加一个简单的数据契约:

namespace ClassLibrary1
{
    using System.Runtime.Serialization;

    [DataContract]
    public class Class1
    {
    }
}

编译器告诉我:

The type or namespace name 'DataContract' could not be found (are you missing a using directive or an assembly reference. It appears the namespace System.Runtime.Serialization is missing when .NET 4.6 is selected as the target.

当为目标选择 .NET Framework 4.6 时,似乎没有可用的序列化。如果我退回到 .NET 4.5.1,则可以编译相同的代码(并在更复杂的项目中运行)。这里发生了什么? .NET 4.6 还没有为 Visual Studio 中的 Prime Time 做好准备吗?还有其他人遇到过这个吗?

最佳答案

这里有同样的问题,看来解决方案是将相关的 NuGet 包添加到包含已移出 Core 的功能的项目中。具体来说,您需要 Serialization Primitives ,但我在下面包含了 project.json 文件,它可能更接近您在实际配置(依赖项等)方面想要的内容

This site还有一个用于 .NET 5 包的各种“搜索引擎”,这基本上就是您在这里所做的。

{
"supports": {
    "net46.app": {},
    "uwp.10.0.app": {},
    "dnxcore50.app": {}
  },
  "dependencies": {
    "Microsoft.NETCore": "5.0.0",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.0",
    "System.Collections": "4.0.10",
    "System.Collections.Specialized": "4.0.0",
    "System.Linq": "4.0.0",
    "System.Linq.Expressions": "4.0.10",
    "System.Linq.Queryable": "4.0.0",
    "System.Net.Requests": "4.0.10",
    "System.Runtime": "4.0.20",
    "System.Runtime.Serialization.Primitives": "4.0.10",
    "System.Runtime.Serialization.Json": "4.0.0",
    "System.Runtime.Serialization.Xml": "4.0.10"
  },
  "frameworks": {
    "dotnet": {
      "imports": "portable-net452+win81"
    }
  }
}

关于c# - 可移植类库在 .NET 4.6 中没有 DataContract 或序列化函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32094383/

相关文章:

c# - 为什么 integer == null 在 C# 中是一个有效的 boolean 表达式?

c# - 为什么分组连接的结果重复 2 次?

android - 使用 Visual Studio 2015 创建 apk 文件

c# - 使用 JsonConvert 进行序列化会丢失数据

c# - [DataContract] 的 JSON 示例

c# - 如何在 Angular 中解码 JWE token

c# - 将套接字从 .NET 传递到非托管 C++ 代码

c# - 禁用 VS2015 C# 编译器自动生成 [Serializable] 属性

c# - 如何阻止 VS2015 将 my.csproj 转换为 .sqlproj?

c# - 当我在 WCF 中使用类/属性时,我应该将它们装饰为 DataContract/DataMember 吗?