c# - Protobuff.net can't serialise Interface -


i'm getting error

the type cannot changed once serializer has been generated

when attempting serialise protobuff.net. i've managed reduce code find culprit, know why it's not able serialise property.

i've found working solution i'm able use, interested in explanation of why code fails.

wont serialise:

[protocontract] public class somecontroller {     [protomember(3)]     public int controllervalue { get; set; }      [protomember(4, asreference = true)]     private itest itestobj { get; set; }      private somecontroller(){}     public somecontroller(object something, int value)     {         controllervalue = value;         itestobj = itest;     } } 

will serialise:

the error caused somecontroller.itestobj. if change class to:

[protocontract] public class somecontroller {     [protomember(3)]     public int controllervalue { get; set; }      [protomember(4, asreference = true)]     private testobj originalobject { get; set; }      private itest itestobj => originalobject itest;      private somecontroller(){}     public somecontroller(testobj something, int value)     {         controllervalue = value;         originalobject = something;     } } 

it works fine.

working code:

below self contained http handler run code , reproduce error:

using system.io; using system.web; using protobuf;  namespace handlers {     /// <summary>     /// summary description test     /// </summary>     public class test : ihttphandler     {         [protocontract]         public class testobj : itest         {             [protomember(1, asreference = true)]             public somecontroller somecontroller { get; set; }              [protomember(2)]             public int somevalue { get; set; }              private testobj(){}             public testobj(int something)             {                 somecontroller = new somecontroller(this, + 1);                 somevalue = something;             }         }          [protocontract]         public class somecontroller         {             [protomember(3)]             public int controllervalue { get; set; }              [protomember(4, asreference = true)]             private itest itestobj { get; set; }              private somecontroller() { }             public somecontroller(object something, int value)             {                 controllervalue = value;                 itestobj = itest;             }         }          [protocontract]         [protoinclude(5, typeof(testobj))]         public interface itest         {             [protomember(6, asreference = true)]             somecontroller somecontroller { get; set; }              [protomember(7)]             int somevalue { get; set; }         }           public void processrequest(httpcontext context)         {             var testobj = new testobj(5);             var serialised = serialiser.serialise(testobj);             var deserialised = serialiser.deserialise<testobj>(serialised);             httpcontext.current.response.write(deserialised.somevalue + "|" + deserialised.somecontroller.controllervalue + "<br>");         }          protected internal class serialiser         {             protected internal static byte[] serialise<t>(t objecttoserialise)             {                 using (var stream = new memorystream())                 {                     serializer.serialize(stream, objecttoserialise);                     return stream.toarray();                 }             }              protected internal static t deserialise<t>(byte[] bytes)             {                 using (var stream = new memorystream(bytes))                 {                     return serializer.deserialize<t>(stream);                 }             }         }          public bool isreusable         {                         {                 return false;             }         }     } } 

interfaces ... awkward. news can give hint in code (before start serializing):

serializer.prepareserializer<itest>(); 

it nice if code detect better in advance, but: right above should help. example, i've put code in static type initializer:

static handler1() {     serializer.prepareserializer<itest>(); } 

but go in global.asax or anywhere else happens before start serializing.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -