c# - XmlSerializer : "The type ' Type' may not be used in this context"

标签 c# xml-serialization

我正在尝试找出如何使用 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继承自 ReactObjectser1被实例化以序列化 ReactObject 的类型, 但从你的代码示例中我只能说 list类型为 ReactListObject .

关于c# - XmlSerializer : "The type ' Type' may not be used in this context",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3897818/

相关文章:

c# - 缩放绘制的曲线

c# - IDisposable 对象是否在使用 RequestContainer 的 Nancy 请求结束时被释放?

java - “HTML 属性不可序列化”异常 (java)

c# - 未填充 Linq to Sql 对象

c# - 日志框架,一个好主意?

c# - 为什么序列化 double[] 在 WinCE 中不起作用?

c# - 如何使用 asp.net 序列化反序列化具有值和属性的 xml 节点

.net - 从具有不同类型的 HttpWebResponse GetResponseStream 反序列化 XML

c# - System.Environment.OSVersion 返回错误的版本

UWP 中的 C# XML 序列化