![](/img/trans.png)
[英]ProtoBuf-net invalid behaviour when serializing object containing list
[英]Protobuf-net: serializing a custom class containing a Dictionary<object, object>
我正在使用Marc Gravell的ProtoBuf-net庫(r480,net20)來序列化/反序列化包含在服務器/客戶端場景(C#中)中使用的已知類型的Dictionary <object, object>
的自定義類。
這將使用BinaryFormatter取代我們當前的方法。
作為基礎,我遵循這里提出的建議: protobuf-and-listobject-how-to-serialize -deserialize和protobuf-listobject-how-to-serialize-deserialize 。
目前的方法有一些缺點,我希望更熟悉Protobuf-net的人可以給我一個如何改進它的暗示。
<ProtoObject, ProtoObject>
()調用<ProtoObject, ProtoObject>
Dictionary <object, object>
復制到Dictionary <ProtoObject, ProtoObject>
。 理想情況下,我想使用RuntimeTypeModel方法,但我不知道如何讓客戶端知道類型(編譯和傳輸TypeModel dll到客戶端?)。
同樣在第一個主題中,Marc Gravell提到即將推出的“運行時可擴展模式”可能會有所幫助,是否有人知道這些模式是否已實現以及它們如何工作?
我非常感謝我得到的任何回復,如果我能澄清更多內容,請告訴我。
無論如何,多虧Marc Gravell的神奇圖書館:)。
這是代碼:
[Serializable]
[ProtoContract]
public class Attributes : IXmlSerializable, IEnumerable, IEquatable<Attributes>, ICloneable
{
// Non ProtoBuf-net relevant code was removed
private Dictionary<object, object> attributes = new Dictionary<object, object>();
[ProtoMember(1)]
private Dictionary<ProtoObject, ProtoObject> protoDictionary;
[OnSerializing]
public void OnSerializing(StreamingContext context)
{
this.protoDictionary = new ProtoDictionary();
foreach (var attribute in attributes)
{
this.protoDictionary.Add(ProtoObject.Create(attribute.Key), ProtoObject.Create(attribute.Value));
}
}
[OnDeserialized]
public void OnDeserialized(StreamingContext context)
{
if (this.protoDictionary != null)
{
this.attributes = new SerializableHashtable();
foreach (var o in this.protoDictionary)
{
this.attributes.Add(o.Key.Value, o.Value.Value);
}
}
}
}
[ProtoContract]
[ProtoInclude(1, typeof(ProtoObject<bool>))]
[ProtoInclude(2, typeof(ProtoObject<byte>))]
[ProtoInclude(3, typeof(ProtoObject<sbyte>))]
[ProtoInclude(4, typeof(ProtoObject<ushort>))]
[ProtoInclude(5, typeof(ProtoObject<short>))]
[ProtoInclude(6, typeof(ProtoObject<uint>))]
[ProtoInclude(7, typeof(ProtoObject<int>))]
[ProtoInclude(8, typeof(ProtoObject<ulong>))]
[ProtoInclude(9, typeof(ProtoObject<long>))]
[ProtoInclude(10, typeof(ProtoObject<float>))]
[ProtoInclude(11, typeof(ProtoObject<double>))]
[ProtoInclude(12, typeof(ProtoObject<decimal>))]
[ProtoInclude(13, typeof(ProtoObject<string>))]
[ProtoInclude(20, typeof(ProtoObject<Vector2F>))]
[ProtoInclude(21, typeof(ProtoObject<Vector3F>))]
[ProtoInclude(22, typeof(ProtoObject<Shape>))]
[ProtoInclude(23, typeof(ProtoObject<SharedUser>))]
[ProtoInclude(24, typeof(ProtoObject<SharedShip>))]
//[ProtoInclude(25, typeof(ProtoObject<IVehicleConfiguration>))] // Requires Steering dll -> cyclic reference
[ProtoInclude(26, typeof(ProtoObject<DroneState>))]
[ProtoInclude(27, typeof(ProtoObject<BuffCode>))]
[ProtoInclude(28, typeof(ProtoObject<ItemAttribute>))]
[ProtoInclude(40, typeof(ProtoObject<List<int>>))]
public abstract class ProtoObject
{
protected ProtoObject()
{
}
// Replaces public static ProtoObject<T> Create<T>(T value)
// in order to use the actual type of the object
public static ProtoObject Create(object obj)
{
if (obj is bool)
{
return new ProtoObject<bool>((bool)obj);
}
if (obj is byte)
{
return new ProtoObject<byte>((byte)obj);
}
// etc. for all required types
return null;
}
public static ProtoObject Create(bool obj)
{
TypeModel.Add(obj.GetType(), true);
return new ProtoObject<bool>(obj);
}
public static ProtoObject Create(byte obj)
{
return new ProtoObject<byte>(obj);
}
// ... public static ProtoObject Create(type obj) -> for all required types
public object Value
{
get { return ValueImpl; }
set { ValueImpl = value; }
}
protected abstract object ValueImpl { get; set; }
}
[ProtoContract]
public sealed class ProtoObject<T> : ProtoObject
{
public ProtoObject()
{
}
public ProtoObject(T value)
{
Value = value;
}
[ProtoMember(1)]
public new T Value { get; set; }
protected override object ValueImpl
{
get { return Value; }
set { Value = (T)value; }
}
public override string ToString()
{
return Value.ToString();
}
}
序列化Dictionary<object,object>
根本就不是一個受支持的用例...我個人認為你應該更多地考慮使用特定於用途的DTO模型,就像使用XmlSerializer
, DataContractSerializer
或JavascriptSerializer
。 protobuf-net最終仍然是一個合同序列化器,DTO模型是理想的用例。 它通常適用於非DTO模型,但這不是一個開放的保證,它將適用於每個可能設計的模型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.