2015-04-03 77 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來根據自己的喜好配置序列化程序。