我有一个实现ISerializable
的类X.#region ISerializable Members
/// <summary>
/// Sets up for deserialization
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
private X(SerializationInfo info, StreamingContext context)
{
this.Key = info.GetString("Key");
this.Title = info.GetString("Title");
this.Count = info.GetInt32("Count");
originalDataSource = new ObservableDataSource<IDataType>((IDataType[])info.GetValue("DataSource", typeof(IDataType[])));
this.A = (Color)ColorConverter.ConvertFromString(info.GetString("A"));
this.B = Utilities.GetDashStyleFromString(info.GetString("B"));
this.C = info.GetDouble("C");
this.D = (Color)ColorConverter.ConvertFromString(info.GetString("D"));
this.E = (Shape)Enum.Parse(typeof(Shape), info.GetString("E"));
this.F = info.GetInt32("F");
this.G = info.GetInt32("G");
this.H = info.GetInt32("H");
this.I = info.GetBoolean("I");
}
/// <summary>
/// Sets up for serialization
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Key", this.Key);
info.AddValue("Title", this.Title);
info.AddValue("Count", this.Count);
info.AddValue("DataSource", this.originalDataSource.Collection.ToArray<IDataType>());
info.AddValue("A", this.A.ToString());
info.AddValue("B", this.B.ToString());
info.AddValue("C", this.C);
info.AddValue("D", this.D.ToString());
info.AddValue("E", this.E);
info.AddValue("F", this.F);
info.AddValue("G", this.G);
info.AddValue("H", this.H);
info.AddValue("I", this.I);
}
#endregion
我有一个Y类,它包含一个List和一个eventaggregator。
#region ISerializable Members
/// <summary>
/// Sets up for deserialization
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
private Y(SerializationInfo info, StreamingContext context)
{
List<X> Coll = (List<X>)info.GetValue("DataSource", typeof(List<X>));
if (_Collection == null)
_Collection = new List<X>();
foreach (X x in Coll)
_Collection.Add(x);
Count = info.GetInt32("Count");
}
/// <summary>
/// Sets up for serialization
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("DataSource", _Collection);
info.AddValue("Count", this.Count);
}
#endregion
我需要用二进制序列化Y.
但是当我反序列化Y并检查以下行时,它返回一个带有空值的List来代替应该被序列化的X对象。
List<X> Coll = (List<X>)info.GetValue("DataSource", typeof(List<X>));
知道这段代码有什么问题吗?我无法简单地使用条件序列化进行序列化,Color不会被序列化,并且ObservableDataSource也没有Serializable属性,我无法修改它。
这是在.NET framework 3.5中(如果有任何用处)。
答案 0 :(得分:1)
Color类不可序列化,您必须为它实现自己的自定义序列化格式(我通常会序列化ARGB代码)。
除此之外,BinarySerializer支持集合序列化,因此您实际上不需要实现自己的序列化函数。