c# - Can ProtoBuf-Net deserialize to a flat class? -



c# - Can ProtoBuf-Net deserialize to a flat class? -

protobuf-net uses nested protobuf constructs back upwards inheritance. however, can made force properties flat target class has same properties inherited "serialized" version?

see test illustration below. needless result of flat namespace null both properties.

possible solution: re-create info flat.b first on property property basis. note: not prefered option.

using system; namespace hierarchy { using protobuf; [protocontract] public class { [protomember(1)] public string prop1 { get; set; } } [protocontract] public class b : { public b() { } [protomember(1)] public string prop2 { get; set; } public override string tostring() { homecoming "prop1=" + prop1 + ", prop2=" + prop2; } } } namespace flat { using protobuf; [protocontract] public class b { [protomember(1)] public string prop1 { get; set; } [protomember(2)] public string prop2 { get; set; } public override string tostring() { homecoming "prop1=" + prop1 + ", prop2=" + prop2; } } } namespace testprotoserialization { using protobuf; using system.io; public class test2 { public void test() { var hb = new hierarchy.b(); hb.prop1 = "prop1"; hb.prop2 = "prop2"; var ms = new memorystream(); serializer.serialize<hierarchy.b>(ms, hb); var flatb = serializer.deserialize<flat.b>(ms); console.writeline(hb.tostring()); // <----- output: prop1=prop1, prop2=prop2 console.writeline(flatb.tostring()); // <----- output: prop1=, prop2= } } public class programme { private static void main(string[] args) { var o2 = new test2(); o2.test(); } } }

not directly, , i'm not sure there great need to. maybe missing in example...

to pick on key point - forgetting inheritance you've broken contract - te fields in exampl 1 & 1 in 1 model , 1 & 2 in other.

it depends objective is; if want force info over, sure can set runtimetypemodel only knows derived type (disable automatic configuration , add together fields manually). work derived type (obviously), output info expected flat model:

var model = typemodel.create(); model.add(typeof(b), false) .add("prop1", "prop2");

then utilize model.serialize etc.

however, writing flat conversion method on c#, or using automapper more obvious. utilize above if objective remove inheritance output, illustration interoperability reasons.

c# protobuf-net

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -