在 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/