NetStandard 1.4 不允许用 [DataContract] 修饰 class
NetStandard 1.4 does not allow decorating class with [DataContract]
当我尝试在 .NETStandard 1.4 项目中用 [DataContract]
修饰 class 时
[DataContract]
[KnownType(typeof(SingleSensorEvent))]
[KnownType(typeof(BatchSensorEvent))]
public abstract class WebItemBase
{
// details left out
}
我得到以下编译错误
1>WebItemBase.cs(7,6,7,18): error CS0653: Cannot apply attribute class
'DataContract' because it is abstract
1>WebItemBase.cs(8,6,8,15):
error CS0012: The type 'Attribute' is defined in an assembly that is
not referenced. You must add a reference to assembly 'mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
有没有办法在 NETStandard 1.4 中使用数据协定?
您需要添加对 System.Runtime.Serialization.Primitives
NuGet 包的引用才能使用 DataContractAttribute
。它将在 netstandard2.0
:
中自动可用
dotnet add package System.Runtime.Serialization.Primitives
当我尝试在 .NETStandard 1.4 项目中用 [DataContract]
修饰 class 时
[DataContract]
[KnownType(typeof(SingleSensorEvent))]
[KnownType(typeof(BatchSensorEvent))]
public abstract class WebItemBase
{
// details left out
}
我得到以下编译错误
1>WebItemBase.cs(7,6,7,18): error CS0653: Cannot apply attribute class 'DataContract' because it is abstract
1>WebItemBase.cs(8,6,8,15): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
有没有办法在 NETStandard 1.4 中使用数据协定?
您需要添加对 System.Runtime.Serialization.Primitives
NuGet 包的引用才能使用 DataContractAttribute
。它将在 netstandard2.0
:
dotnet add package System.Runtime.Serialization.Primitives