¿Qué hace "applyDefaultBehaviour" en el método RuntimeTypeModel.Add?

In protobuf-net, the Add método en ProtoBuf.Meta.RuntimeTypeModel toma dos argumentos, un System.Type y un booleano applyDefaultBehaviour - What does the boolean do? How does it affect things? When do I need to pass it true/false?

The xml doc says:

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)

That wasn't enough for me to understand, maybe it's just me...

Gracias por cualquier ayuda.

preguntado el 28 de mayo de 14 a las 13:05

1 Respuestas

Si pasa false, then protobuf-net will do nada to configure the type. It won't look at attributes, it won't look for common patterns. It will infer that you want to do all of that yourself, to build a custom model (perhaps for versioning reasons, or because the type is outside of your control and has attributes that are confusing protobuf-net).

Basically, pass true unless you know what you are doing ;p

Continuing from the comments: I se mostró plenamente recommend that you simply swap to a separate DTO model. Serialize the data, not the implementation. As soon as I hear "Unity3D classes", my default is "that probablemente no es la mejor opción.

But as an example of manual configuration, here's a handy trick if SomeType (belonging to unity) cannot be serialized:

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

donde MyDTOThatLooksLikeSomeType is a regular DTO with attributes etc, but which is protobuf-net friendly (just regular serialization attributes), plus which has conversion operators to/from SomeType. Or you could do things manually:

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

contestado el 28 de mayo de 14 a las 13:05

Thanks for your answer. So if I don't own a type and want to add it to a model I have to pass false? (This is what I'm trying to know since I'm trying to use protobuf in Unity3D where I don't own a lot of its classes of course...) - Can you please say what type of configuration I must do if I pass false? " It will infer that you want to do all of that yourself" - could you define "all of that". Thank you. - vejar

Excuse my ignorance but I'm new to protobuf. What is a DTO - Isn't what you described for me to do is called a surrogate? - So let's say I wanted to serialize UnityEngine.Vector3, I create my own wrapper/surrogate class that looks like a Vector3 with pretty much all the annotations needed by protobuf and provide operator overloads between the two. Right? - vejar

@vexe yes, the surrogate would be a DTO - by "DTO" I simply mean "a simple type whose main job is to transfer data, perhaps for serialization". As for whether to choose a surrogate vs manual configuration: that depends a bit on exactamente qué Vector3 looks like ;p If it no puede be made to play nicely, then a surrogate might be an option. The other option is to create an entire serialization model that no usa Vector3 - marc gravell

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.