我正在尝试使用XmlSerializer序列化任何类,而不使用XmlInclude属性。通常情况下可以工作,但是在以下代码中我会遇到异常:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
namespace Test
{
public class ReactObject { }
public class ReactListObject : ReactObject { }
public class ReactList<T> : ReactObject, ICollection, IList<T>
{
public ReactList() { }
public virtual void AddRange(IEnumerable<T> values) { }
[XmlArray(ElementName = "Items", IsNullable = false)]
public T[] Items
{
get { lock (SyncRoot) { return (ItemsList.ToArray()); } }
set { lock (SyncRoot) { ItemsList = new List<T>(value); } }
}
protected List<T> ItemsList = new List<T>();
public bool IsSynchronized { get { return (true); } }
public object SyncRoot { get { return (mRootObject); } }
private object mRootObject = new object();
public void CopyTo(Array array, int index) { CopyTo(array, index); }
[XmlIgnore()]
public T this[int index] {
get { return (ItemsList[index]); }
set { ItemsList[index] = value; }
}
public int IndexOf(T item) { return (ItemsList.IndexOf(item)); }
public virtual void Insert(int index, T item) { }
public virtual void RemoveAt(int index) { }
public int Count { get { lock (SyncRoot) { return (ItemsList.Count); } } }
public bool IsReadOnly { get { return (false); } }
public virtual void Add(T item) { ItemsList.Add(item); }
public void Clear() { }
public bool Contains(T item) { return (false); }
public virtual void CopyTo(T[] array, int arrayIndex) { }
public virtual bool Remove(T item) { return (false); }
public IEnumerator<T> GetEnumerator() { return (ItemsList.GetEnumerator()); }
IEnumerator IEnumerable.GetEnumerator() { return (ItemsList.GetEnumerator()); }
}
[XmlInclude(typeof(B))]
public class A : ReactListObject
{
public int AValue = 1;
}
public class B : A
{
public int BValue = 2;
}
public class AList : ReactList<A>
{
}
static class Program
{
[STAThread]
static void Main()
{
// NOT good serializer for the type AList
XmlSerializer ser1 = new XmlSerializer(typeof(ReactObject), new Type[] { typeof(A), typeof(B), typeof(ReactList<A>), typeof(AList) });
// Good serializer for the type AList
XmlSerializer ser2 = new XmlSerializer(typeof(ReactList<A>), new Type[] { typeof(A), typeof(B), typeof(AList) });
AList list = new AList();
list.Add(new A());
list.Add(new B());
using (FileStream mStream = new FileStream("C:\\Test.xml", FileMode.Create)) {
ser2.Serialize(mStream, list);
}
using (FileStream mStream = new FileStream("C:\\Test.xml", FileMode.Create)) {
ser1.Serialize(mStream, list);
}
}
}
}
抛出的异常是
类型Test.AList不能在此上下文中使用
这是类图,有助于代码阅读
Develop我想更多地谈谈我试图实现的目标:我希望尽量减少创建XML序列化程序的实例(实际上,每个序列化的类型都有自己的序列化程序;每当需要序列化未包含的类型时,应用程序将重新创建唯一存在的序列化程序。 (*)
在我看来,似乎
- 可以使用类型为ReactObject的序列化程序对任何ReactObject(和派生类型)进行序列化,直到在派生层次结构中引入通用类为止。序列化程序的额外类型应涵盖所有预期类型。
- 在基于ReactObject的泛型类派生类型的情况下,该类型无法使用类型为ReactObject的序列化程序进行序列化,但可以使用泛型类型的序列化程序进行序列化。
这可能是个问题,因为当我需要反序列化它时,我必须知道序列化对象的类型。相反,要反序列化“非泛型”ReactObject类型,只需使用类型为ReactObject的常规序列化程序即可,无需知道序列化对象的确切类型。
(*) 实际上,我不知道这个目标是否会带来明显的改进。核心问题将是“对于所有(包含的)类型,单个序列化程序程序集是否更好,还是每种类型一个序列化程序程序集更好?”