Protobuf-net无法仅使用getter来序列化属性-无法将更改应用于属性 [英] Protobuf-net fails to serialize property with only getter - Cannot apply changes to property
问题描述
我正在使用protobuf-net序列化对象,但出现异常:
I am using protobuf-net to serialize an object and I get exception:
无法将更改应用于属性TestProject.TestMessage.ClientId
Cannot apply changes to property TestProject.TestMessage.ClientId
with stacktrace:
with 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
,等等.尝试构建明确要求此类属性进行序列化的合同时,将引发异常.
Yes, this is correct: protobuf-net cannot successfully round-trip a get-only property such as your ClientId
, and so will throw an exception trying to construct a contract that explicitly requires such a property to be serialized.
在此限制中并不孤单.我注意到您正在使用数据协定属性标记您的类型.如果我尝试使用 DataContractSerializer
序列化其实例,则它将失败,并出现等效的异常:
It is not alone in this limitation. I notice that you are marking your type with data contract attributes. If I try to serialize an instance of it with DataContractSerializer
, it fails with an equivalent exception:
System.Runtime.Serialization.InvalidDataContractException was caught
Message="No set method for property 'ClientId' in type 'Question40276317.V1.TestMessage'."
Source="System.Runtime.Serialization"
如果您只是想跳过仅获取属性,请使用 [IgnoreDataMember]
进行标记.如果要成功对其进行序列化和反序列化,则可以使用以下选项.
If you simply want to skip get-only properties then mark them with [IgnoreDataMember]
. If you want to serialize and deserialize them successfully, you have the following options.
首先,protobuf-net需要能够构造您的对象.与Json.NET不同,它不会调用参数化的构造函数,因此最简单的操作是添加无参数的构造函数.只要存在,它就可以是私有的或受保护的(在整个框架上).或者,您可以设置 [ProtoContract(SkipConstructor = true)]
,但这并不适用于所有框架或部分信任情况.
Firstly, protobuf-net needs to be able to construct your object. Unlike Json.NET it will not call a parameterized constructor so the simplest thing to do is to add a parameterless constructor. It could be private or protected (on the full framework) as long as it exists. Alternatively you could set [ProtoContract(SkipConstructor = true)]
, however that doesn't work on all frameworks or in partial trust situations.
接下来,您需要以某种方式使属性可设置 .一种解决方案是添加私人二传手:
Next, you need to make the properties settable somehow. One solution would be to add private setters:
[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;
}
}
另一种方法是标记基础字段,而不是使用数据协定属性标记属性:
Another would be to mark the underlying fields rather than the properties with data contract attributes:
[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
的值不可变,则需要序列化代理,如下所示:>
Alternatively, if you really want the values for clientId
and name
to be immutable after construction, you're going to need a serialization surrogate, like so:
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; }
}
然后,在序列化之前,请执行以下操作:
Then, before serialization, do:
ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(TestMessage), true).SetSurrogate(typeof(TestMessageSurrogate));
通过使用代理,您还可以避免使用任何无参数的构造函数.
By using a surrogate, you also avoid the need for any parameterless constructor.
这篇关于Protobuf-net无法仅使用getter来序列化属性-无法将更改应用于属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!