, tel biçim yalnızca gönderir öğeler için veri, yani listenin boş mu yoksa boş mu olduğunu bilmek için, o bilgi akışını akışa eklemelisiniz. Orijinal koleksiyon boş olup olmadığını göstermek için ekstra özellik ekleme
kolaydır ama orijinal tip tanımını değiştirmek istemiyorsanız başka iki seçenek vardır: Vekil
vekil türünün kullanılması
serialize ekstra özelliğe sahip olacak (orijinal türünüz el değmemiş durumda olacak) ve listenin orijinal durumunu geri yükleyecektir: null, öğelerle veya boş.
[TestMethod]
public void SerializeEmptyCollectionUsingSurrogate_RemainEmpty()
{
var instance = new SomeType { Items = new List<int>() };
// set the surrogate
RuntimeTypeModel.Default.Add(typeof(SomeType), true).SetSurrogate(typeof(SomeTypeSurrogate));
// serialize-deserialize using cloning
var clone = Serializer.DeepClone(instance);
// clone is not null and empty
Assert.IsNotNull(clone.Items);
Assert.AreEqual(0, clone.Items.Count);
}
[ProtoContract]
public class SomeType
{
[ProtoMember(1)]
public List<int> Items { get; set; }
}
[ProtoContract]
public class SomeTypeSurrogate
{
[ProtoMember(1)]
public List<int> Items { get; set; }
[ProtoMember(2)]
public bool ItemsIsEmpty { get; set; }
public static implicit operator SomeTypeSurrogate(SomeType value)
{
return value != null
? new SomeTypeSurrogate { Items = value.Items, ItemsIsEmpty = value.Items != null && value.Items.Count == 0 }
: null;
}
public static implicit operator SomeType(SomeTypeSurrogate value)
{
return value != null
? new SomeType { Items = value.ItemsIsEmpty ? new List<int>() : value.Items }
: null;
}
}
Kişisel Türleri Genişletilebilir olun
protobuf-net alanlar kırma şey olmadan bir mesaj eklenebilir böylece türlerini uzatmak için izin IExtensible arayüzü önermek (here devamı) . Protobuf-net uzantısını kullanabilmek için, Extensible
sınıfını miras alabilir veya miras kısıtlamasını önlemek için IExtensible
arabirimini uygulayabilirsiniz.
Şimdi türünüzün "genişletilebilir" olduğunu düşünüyorsanız, [OnSerializing]
ve [OnDeserialized]
tanımlarını kullanarak, akışa serileştirilecek yeni göstergelerin eklenmesi ve nesneyi orijinal durumuyla yeniden yapılandırırken bunlardan serileştirilmesi. Artıları, yeni özellikler veya yeni türler için vekil olarak tanımlamanız gerekmediği için, türünüzün tür modelinizde tanımlanmış alt türleri varsa, IExtensible
'un desteklenmemesidir.
[TestMethod]
public void SerializeEmptyCollectionInExtensibleType_RemainEmpty()
{
var instance = new Store { Products = new List<string>() };
// serialize-deserialize using cloning
var clone = Serializer.DeepClone(instance);
// clone is not null and empty
Assert.IsNotNull(clone.Products);
Assert.AreEqual(0, clone.Products.Count);
}
[ProtoContract]
public class Store : Extensible
{
[ProtoMember(1)]
public List<string> Products { get; set; }
[OnSerializing]
public void OnDeserializing()
{
var productsListIsEmpty = this.Products != null && this.Products.Count == 0;
Extensible.AppendValue(this, 101, productsListIsEmpty);
}
[OnDeserialized]
public void OnDeserialized()
{
var productsListIsEmpty = Extensible.GetValue<bool>(this, 101);
if (productsListIsEmpty)
this.Products = new List<string>();
}
}