c# - How to Serialize a Collection of Interface Objects Using Protobuf-Net -


i have following class definitions:

[protoinclude(2, typeof(foo))] public interface ifoo {     double bar { get; } }    [protocontract] public class foo : ifoo {     [protomember(1)]     private double _bar     {         { return bar / 10; }         set { bar = 10 * value; }     }      public double bar { get; private set; } }    [protocontract] public class myclass {     [protomember(1, overwritelist = true)]     public ireadonlylist<ifoo> foos { get; private set; } } 

when try serialize myclass object using protobuf-net, exception:

system.invalidoperationexception : not possible prepare serializer for: mynamespace.myclass ----> system.invalidoperationexception : no serializer defined type: mynamespace.ifoo

in case, know concrete type of items stored in myclass.foos foo. how can tell protobuf use type foo anywhere sees type ifoo? alternatively, how can make include foo 1 of classes available implement ifoo in collection?

-- edit --

the answer sam close enough such revealed issue approach. namely, it not possible serialize property of type ireadonlylist<t> using protobuf-net. there simple workaround, however, since list being created in constructor of myclass. hence, myclass can change following:

[protocontract] public class myclass {     [protomember(1, overwritelist = true)]     private list<ifoo> mutablefoos { get; set; }      public ireadonlylist<ifoo> foos     {         { return mutablefoos; }     } } 

however, serialization of myclass still failing message, "system.invalidoperationexception : no suitable conversion operator found surrogate: mynamespace.ifoo / mynamespace.foo".


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -