我正在尝试找出如何使用 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);
}
}
}
}
异常抛出是
The type Test.AList may not be used in this context
这是类图,以帮助阅读代码
大Develop我想多谈谈我要实现的目标:我想尽量减少 XML 序列化器实例的创建(实际上,每个序列化的类型都有自己的序列化器;应用程序将重新创建唯一存在的每次不包含的类型都应序列化的序列化程序。(*)
看来,恕我直言,
- 可以使用 ReactObject 类型的序列化程序序列化任何 ReactObject(和派生类型),直到在派生层次结构中引入通用类。序列化程序的额外类型应涵盖所有预期类型。
- 如果类型派生自基于 ReactObject 的泛型类,则该类型无法使用类型为 ReactObject 的序列化程序进行序列化,但使用通用类型的序列化器。
这可能是个问题,因为当我需要反序列化时,我必须知道序列化对象的类型。相反,要反序列化“非泛型”ReactObject 类型,使用 ReactObject 类型的通用序列化器就足够了,而无需知道序列化对象的确切类型。
(*) 其实我不知道这个目标会带来合理的改进。核心问题是“对所有(包含的)类型使用一个序列化程序集更好,还是对每个类型使用一个序列化程序程序集更好?”
最佳答案
是否 ReactListObject
继承自 ReactObject
? ser1
被实例化以序列化 ReactObject
的类型, 但从你的代码示例中我只能说 list
类型为 ReactListObject
.
关于c# - XmlSerializer : "The type ' Type' may not be used in this context",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3897818/