c# - Deserialization of an array always gives an array of nulls -
i have custom abstract base class sub classes i've made serializable/deseriablizeable iserializable. when serialization/deserialization of single instances of class' sub classes, works fine. however, when array of them end array of nulls on deserialization. serialization done binaryformatter.
the items contained within a:
public observablecollection<trade> trades { get; private set; }
on serialization done in getobjectdata on serializationinfo parameter:
trade[] trades = (trade[])trades.toarray<trade>(); info.addvalue("trades", trades);
and on deserialization done in serialization constructor on serializationinfo parameter:
trade[] trades = (trade[])info.getvalue("trades", typeof(trade[])); foreach (trade t in trades) { trades.add(t); }
deserialization gives me array of nulls , mentioned earlier, single item serializes , deseriaizes fine code:
serialization (getobjectdata method):
info.addvalue("trade", trades.first<trade>());
deserialization (serialization constructor):
trade t = (trade)info.getvalue("trade", typeof(trade)); trades.add(t);
is common problem? seem find no occurrences of else running in @ least. there solution :) , if need supply more information/code tell me.
thanks!
array deserializes first. inner deserialization done. when try access items, null.
and idea use [ondeserialized]
attribute on method, builds other properies. , here example:
[serializable] public class testclass : iserializable { private trade[] _innerlist; public observablecollection<trade> list { get; set; } public testclass() { } [ondeserialized] private void setvaluesondeserialized(streamingcontext context) { this.list = new observablecollection<trade>(_innerlist); this._innerlist = null; } protected testclass(serializationinfo info, streamingcontext context) { var value = info.getvalue("inner", typeof(trade[])); this._innerlist = (trade[])value; } public void getobjectdata(serializationinfo info, streamingcontext context) { info.addvalue("inner", this.list.toarray()); } }
Comments
Post a Comment