c# - Serializing custom IEnumerable<T> as field not working -


i have custom collection looks this:

class specialreadonlycollection<t> : ireadonlycollection<t>{     private readonly list<t> entries;     public specialreadonlycollection(ienumerable<t> source){         entries = new list<t>(source);     }     ... } 

that (among other things) wraps list doesn't offer add method.

now have 2 other classes:

class a{     public string name;     public int value; }  class containerofa{     public specialreadonlycollection<a> list;      public containerofa(ienumerable<a> source){         this.list = new specialreadonlycollection<a>(source);     } } 

i want serialize containerofa. since don't attributes, how build model , try serialize.

    // make serializable var metatype = model.add(typeof(a),true); metatype.addfield(1,"name"); metatype.addfield(2,"value"); metatype.useconstructor = false;  // make specialcollection serializable model.add(typeof(specialreadonlycollection<a>),true).addfield(1,"entries"); model[typeof(specialreadonlycollection<a>)].ignorelisthandling = true; model[typeof(specialreadonlycollection<a>)].useconstructor = false;  // make container serializable model.add(typeof(containerofa),true).addfield(1,"list"); model[typeof(containerofa)].useconstructor = false;   // initialize container a = new a{name ="name", value =1}; a[] arr = {a}; var container = new containerofa(arr);  // try , serialize ....  model.deepclone(container); 

however, when try serialize, exception:

unable resolve suitable add method specialreadonlycollection[a] 

what find weird if try serialize list works fine:

model.deepclone(container.list); 

everything works fine if instead of building model in code use attributes. in fact, works if use attributes in containerofa , make a , specialreadonlycollection serializable via code.

is there doing wrong? how can around this? (probably easiest answer use attributes ... want avoid attributes).

tl;dr: how to, via runtimetypemodel, use protobuf-net serialize class has member ienumerable should not treated list (ignorelisthandling = true).

protobuf-net supports range of list patterns; in case, trying use ienumerable<t>/getenumerator() , add(t) pattern. needs both of things: ienumerable<t>/getenumerator() used serialization, , add(t) used deserialization. if custom collection not have obvious add(t) method, protobuf-net refuse work collection, because it won't ever able deserialize. method telling you.

the way around mutable dto model, rather read-only collection. there are things implicit type conversions protobuf-net supports, not scenario.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -