2012-01-16 46 views
2

我在尝试使用protobuf序列化类层次结构时遇到了一些问题。继承自抽象类的类未实现的属性没有得到正确的值。例如,当我尝试测试以下层次...:如何用抽象类使用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> 
{ 
} 

所有NannyTutorChild的反序列化istances被设置为空,即使他们叫Serializer.DeepClone之前正确增值的()。我发现实现它的唯一方法是标记为Tutor类中的属性,并在NannyTutorChild中实现它们。

我知道这可能看起来愚蠢的层次,但在我们的实际项目中,我们有很多来自我们的“导师” classm派生不同的类和所有的水平,以强制转换或使用正确的方法:)

需要

有什么想法?难道我做错了什么?

谢谢大家, Mat。

回答

2

目前不支持此方案; DynamicType和继承的组合存在复杂性,我仍然需要解决;我会在我正在等待工作的队列中向前迈进,但是:它今天不在。

+0

谢谢!我会实现使用抽象属性:) 保持良好的工作! – 2012-01-16 23:17:42

+0

@VollmonD强调,带继承的DynamicType组合是关键问题,我认为 – 2012-01-16 23:56:03