c# – Protobuf-net无法仅使用getter序列化属性 – 无法对属性应用更改
作者:互联网
我使用protobuf-net来序列化一个对象,我得到了异常:
无法对属性TestProject.TestMessage.ClientId应用更改
使用stacktrace:
at ProtoBuf.Serializers.PropertyDecorator.SanityCheck(TypeModel model, PropertyInfo property, IProtoSerializer tail, Boolean& writeValue, Boolean nonPublic, Boolean allowInternal)
at ProtoBuf.Serializers.PropertyDecorator..ctor(TypeModel model, Type forType, PropertyInfo property, IProtoSerializer tail)
at ProtoBuf.Meta.ValueMember.BuildSerializer()
at ProtoBuf.Meta.ValueMember.get_Serializer()
at ProtoBuf.Meta.MetaType.BuildSerializer()
at ProtoBuf.Meta.MetaType.get_Serializer()
at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest)
at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value)
at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context)
at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value)
at ProtoBuf.Serializer.Serialize[T](Stream destination, T instance)
我的课程如下:
[DataContract]
public class TestMessage
{
private int clientId;
[DataMember(Order = 1)]
public int ClientId
{
get { return clientId; }
}
private string name;
[DataMember(Order = 2)]
public string Name
{
get { return name; }
}
public TestMessage(int clientId,
string name)
{
this.clientId = clientId;
this.name =name;
}
}
解决方法:
是的,这是正确的:protobuf-net无法成功地绕过像ClientId这样的get-only属性,因此会抛出异常,试图构造一个明确要求序列化这样的属性的契约.
在这个限制中并不是唯一的.我注意到您使用数据协定属性标记您的类型.如果我尝试使用DataContractSerializer序列化它的实例,它将失败,并带有等效的异常:
System.Runtime.Serialization.InvalidDataContractException was caught
Message="No set method for property 'ClientId' in type 'Question40276317.V1.TestMessage'."
Source="System.Runtime.Serialization"
如果您只想跳过get-only属性,请使用[IgnoreDataMember]标记它们.如果要成功序列化和反序列化,则可以使用以下选项.
首先,protobuf-net需要能够构建你的对象.与Json.NET不同,它不会调用参数化构造函数,因此最简单的方法是添加无参数构造函数.只要它存在,它可以是私有的或受保护的(在完整框架上).或者,您可以设置[ProtoContract(SkipConstructor = true)]
,但这不适用于所有框架或部分信任情况.
接下来,您需要以某种方式设置属性.一种解决方案是添加私有的setter:
[DataContract]
public class TestMessage
{
private int clientId;
[DataMember(Order = 1)]
public int ClientId
{
get { return clientId; }
private set { clientId = value; }
}
private string name;
[DataMember(Order = 2)]
public string Name
{
get { return name; }
private set { name = value; }
}
protected TestMessage() { }
public TestMessage(int clientId, string name)
{
this.clientId = clientId;
this.name = name;
}
}
另一种方法是标记基础字段而不是具有数据协定属性的属性:
[DataContract]
public class TestMessage
{
[DataMember(Name = "ClientId", Order = 1)]
private int clientId;
public int ClientId
{
get { return clientId; }
}
[DataMember(Name = "Name", Order = 2)]
private string name;
public string Name
{
get { return name; }
}
protected TestMessage() { }
public TestMessage(int clientId, string name)
{
this.clientId = clientId;
this.name = name;
}
}
或者,如果您确实希望clientId和name的值在构造后是不可变的,那么您将需要一个序列化代理,如下所示:
public class TestMessage
{
private readonly int clientId;
public int ClientId
{
get { return clientId; }
}
private readonly string name;
public string Name
{
get { return name; }
}
public TestMessage(int clientId, string name)
{
this.clientId = clientId;
this.name = name;
}
}
[DataContract]
internal class TestMessageSurrogate
{
public static implicit operator TestMessageSurrogate(TestMessage message)
{
if (message == null)
return null;
return new TestMessageSurrogate { ClientId = message.ClientId, Name = message.Name };
}
public static implicit operator TestMessage(TestMessageSurrogate message)
{
if (message == null)
return null;
return new TestMessage(message.ClientId, message.Name);
}
[DataMember(Order = 1)]
public int ClientId { get; set; }
[DataMember(Order = 2)]
public string Name { get; set; }
}
然后,在序列化之前,执行:
ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(TestMessage), true).SetSurrogate(typeof(TestMessageSurrogate));
通过使用代理,您还可以避免使用任何无参数构造函数.
标签:c,serialization,protocol-buffers,protobuf-net 来源: https://codeday.me/bug/20190627/1309458.html