2015-04-03 78 views
4

我有一个父类,我想有很多扁平的孩子。这意味着10个或更多不同的类将从一个类中固有。protobuf网从子女继承父母

这是我的。

基类:

[ProtoContract] 
[ProtoInclude(500, typeof(Message1Send))] 
[ProtoInclude(501, typeof(Message2Send))] 
public class MessageBase 
{ 
    [ProtoMember(1)] 
    public string Topic {get;set;} 
    [ProtoMember(2)] 
    public string Action { get; set; }  
} 

很多子类的2:

[ProtoContract]  
public class Message1Send : MessageBase 
{   
    [ProtoMember(1)] 
    public string Property1 { get; set; } 
} 

[ProtoContract]  
public class Message2Send : MessageBase 
{   
    [ProtoMember(1)] 
    public string Property1 { get; set; } 
} 

我希望能够告诉孩子对象我是一个基类的一部分。

我没有得到什么的地步,我的基类如下:

[ProtoContract] 
[ProtoInclude(500, typeof(Message1Send))] 
[ProtoInclude(501, typeof(Message2Send))] 
[ProtoInclude(502, typeof(Message3Send))] 
[ProtoInclude(503, typeof(Message4Send))] 
[ProtoInclude(504, typeof(Message5Send))] 
[ProtoInclude(505, typeof(Message6Send))] 
[ProtoInclude(506, typeof(Message7Send))] 
[ProtoInclude(507, typeof(Message8Send))] 
[ProtoInclude(508, typeof(Message9Send))] 
[ProtoInclude(509, typeof(Message10Send))] 
public class MessageBase 
{ 
    [ProtoMember(1)] 
    public string Topic {get;set;} 
    [ProtoMember(2)] 
    public string Action { get; set; }  
} 

有没有一种方法我可以有发送类的每一个只是一个引用添加到基地类,所以我不必为我创建的每个扁平儿童添加ProtoInclude?

回答

3

问题是可靠性问题之一。反射使得可重复/可靠的保证变得非常重要,如果你今天序列化数据,那么编辑你的应用程序以添加两个新类型是非常重要的,每种类型仍然具有与原始数据相同的编号。即使你添加了一些新的类型,重新命名了一些,并且可能删除了你并未真正使用的两个类型。

该属性通过使字段号码可重复来保证这一点。它是在父母(而不是孩子)的原因是,它是更可靠的步行向上类型链比向下它。但是,如果您有一种可靠的可重复生成子类型字段号的方法,则可以使用RuntimeTypeModel来根据自己的喜好配置序列化程序。