c# - RuntimeTypeModel.Add方法中的 "applyDefaultBehaviour"有什么作用?

标签 c# protobuf-net

在 protobuf-net 中,ProtoBuf.Meta.RuntimeTypeModel 中的 Add 方法采用两个参数,一个 System.Type 和一个 bool 值 applyDefaultBehaviour - bool 值有什么作用?它如何影响事物?我什么时候需要传递真/假?

xml 文档说:

Whether to apply the inbuilt configuration patterns (via attributes etc), or
just add the type with no additional configuration (the type must then be manually configured)

这对我来说还不够理解,也许只是我......

感谢您的帮助。

最佳答案

如果您通过false ,那么 protobuf-net 将什么都不做来配置类型。它不会查看属性,也不会寻找常见模式。它将推断您想要自己完成所有这些操作,以构建自定义模型(可能是出于版本控制原因,或者因为该类型超出您的控制范围并且具有令人困惑的 protobuf-net 属性)。

基本上,通过true除非你知道自己在做什么;p


继续评论:我强烈建议您简单地切换到单独的 DTO 模型。序列化数据,而不是实现。当我听到“Unity3D 类”时,我的默认想法是“可能不是最好的选择。

但作为手动配置的示例,这里有一个方便的技巧 if SomeType (属于unity)无法序列化:

var metaType = model.Add(typeof(SomeType), false);
metaType.SetSurrogate(typeof(MyDTOThatLooksLikeSomeType));

哪里MyDTOThatLooksLikeSomeType是一个具有属性等的常规 DTO,但它 protobuf-net 友好(只是常规序列化属性),另外它还具有与 SomeType 之间的转换运算符。或者您可以手动执行操作:

var metaType = model.Add(typeof(SomeType), false).Add("Foo").Add(12, "Bar");
metaType.UseConstructor = false;

关于c# - RuntimeTypeModel.Add方法中的 "applyDefaultBehaviour"有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23911945/

相关文章:

java - golang为[z][y][x]int定义数组int[x][y][z]有什么好处?

c# - 为什么我的 List<Foo> 不能用 protobuf-net 序列化?

protobuf-net - 重写内置类型的序列化方法

protobuf-net - 确定 Protobuf .NET 中缺失的(必填)字段

c# - 如何更改 datetime.Now 没有特殊字符?

c# - 使用包管理器控制台添加迁移时定义迁移文件夹

c# - 在 Silverlight 应用程序中集成现有的 wcf 项目

c# - Win8.1 上的 MediaElement 内存泄漏

serialization - 如何使用protobuf序列化大型嵌套数组?

c# - 如何使用 protobuf-net 将 C# 日期时间转换为 Python 日期时间?