protobufを使用してクラスの階層をシリアル化しようとするといくつかの問題があります。抽象クラスから継承したクラスによって実装されていないプロパティは、正しい値を取得しません。例えば、私は次の階層をテストしようとすると...:NannyTutorChildのprotobufを使用して抽象クラスで階層をシリアル化する方法
[ProtoContract]
[ProtoInclude(11, typeof(Child))]
[ProtoInclude(12, typeof(Nanny))]
public abstract class Person
{
[ProtoMember(1)]
public string Name { get; set; }
}
[ProtoContract]
public class Child : Person
{
public Child(){ }
}
[ProtoContract]
public class Nanny : Person
{
public Nanny()
{
Tutors = new List<ITutor<Person, Person>>();
}
[ProtoMember(1)]
public List<ITutor<Person, Person>> Tutors { get; set; }
}
[ProtoContract]
[ProtoInclude(11, typeof(NannyTutorChild))]
public interface ITutor<out T, out U>
where T : Person
where U : Person
{
[ProtoMember(1, AsReference = true, DynamicType = true)]
T TutoredBy { get; }
[ProtoMember(2, AsReference = true, DynamicType = true)]
U Tutored { get; }
}
[ProtoContract]
public abstract class Tutor<T, U> : ITutor<T, U>
where T : Person
where U : Person
{
[ProtoMember(1, AsReference = true, DynamicType = true)]
public T TutoredBy { get; set; }
[ProtoMember(2, AsReference = true, DynamicType = true)]
public U Tutored { get; set; }
}
[ProtoContract]
public abstract class NannyTutor<U> : Tutor<Nanny, U>
where U : Person
{
}
[ProtoContract]
public class NannyTutorChild : NannyTutor<Child>
{
}
すべての非直列化されたistancesがnullに設定されている、彼らは正しくSerializer.DeepCloneを呼び出す前に付加価値化しているにもかかわらず、()。私がそれを動作させるために見つけた唯一の方法は、クラスTutorのプロパティを抽象としてマークし、それらをNannyTutorChildに実装することです。
私はそれが階層として愚かに見ることができる知っているが、私たちの本当のプロジェクトで私たちは「同い年の家庭教師」classm由来の異なるクラスの多くを持っているし、すべてのレベルが正しい方法を型キャストまたは使用するために必要な:)
アイデア?私は何か間違っているのですか?
ありがとう皆さん、
ありがとうございました!抽象プロパティを使用して実装します:) 良い仕事を続けてください! –
@ VollmonDは、継承を伴うDynamicTypeの組み合わせが重要な問題だと強調しています –