c# – 从子到父的protobuf-net继承
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 从子到父的protobuf-net继承,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1760字,纯文字阅读大概需要3分钟。
内容图文
![c# – 从子到父的protobuf-net继承](/upload/InfoBanner/zyjiaocheng/787/e4f171ae73514ae695f1588b0867c478.jpg)
我有一个父母班,我想要有很多平坦的孩子.这意味着一个班级将固有10个或更多不同的班级.
这就是我所拥有的.
基类:
[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; }
}
有没有办法让每个Send类只添加一个对基类的引用,所以我不必为我创建的每个平面子项添加ProtoInclude?
解决方法:
问题在于可靠性.反射使得非常可重复/可靠的保证,如果您今天序列化数据,那么编辑您的应用程序以添加两种新类型非常重要,每种类型仍然具有与其相同的数量.即使您添加了一些新类型,也重命名了一些,并且可能删除了两个您并未真正使用的类型.
该属性通过使字段编号可重复来保证这一点.它在父母(而不是孩子)身上的原因是,走向类型链比走向它更可靠.
但是:如果您有可靠的可重复方法为子类型生成字段编号,则可以使用RuntimeTypeModel根据自己的喜好配置序列化程序.
内容总结
以上是互联网集市为您收集整理的c# – 从子到父的protobuf-net继承全部内容,希望文章能够帮你解决c# – 从子到父的protobuf-net继承所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。