使用c# . net 2.0,我有一个组合数据类,它确实有[Serializable]属性。我正在创建一个XMLSerializer类并将其传递到构造函数中:

XmlSerializer serializer = new XmlSerializer(typeof(DataClass));

我得到一个异常说:

有错误反射类型。

在数据类内部有另一个复合对象。这是否也需要有[Serializable]属性,或者通过将它放在顶部对象上,它是否递归地应用到内部的所有对象?


当前回答

我也认为Serializable属性必须在对象上,但除非我是一个完全的新手(我在一个深夜的编码会话中),以下工作来自SnippetCompiler:

using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Xml.Serialization;

public class Inner
{
    private string _AnotherStringProperty;
    public string AnotherStringProperty 
    { 
      get { return _AnotherStringProperty; } 
      set { _AnotherStringProperty = value; } 
    }
}

public class DataClass
{
    private string _StringProperty;
    public string StringProperty 
    { 
       get { return _StringProperty; } 
       set{ _StringProperty = value; } 
    }

    private Inner _InnerObject;
    public Inner InnerObject 
    { 
       get { return _InnerObject; } 
       set { _InnerObject = value; } 
    }
}

public class MyClass
{

    public static void Main()
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(DataClass));
            TextWriter writer = new StreamWriter(@"c:\tmp\dataClass.xml");
            DataClass clazz = new DataClass();
            Inner inner = new Inner();
            inner.AnotherStringProperty = "Foo2";
            clazz.InnerObject = inner;
            clazz.StringProperty = "foo";
            serializer.Serialize(writer, clazz);
        }
        finally
        {
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
    }

}

我可以想象XmlSerializer在公共属性上使用反射。

其他回答

当我创建一个具有数据类型-类型的属性时,我得到了相同的错误。在此,我得到了一个错误-有一个错误反射类型。我不断检查'InnerException'的每个异常从调试码头和得到特定的字段名(这是类型)在我的情况下。解决方法如下:

    [XmlIgnore]
    public Type Type { get; set; }

我认为最常见的原因是:

 - the object being serialized has no parameterless constructor
 - the object contains Dictionary
 - the object has some public Interface members

到目前为止,我有一个稍微不同的解决方案,所以对于任何未来的文明,这里是我的!

我已经声明了一个“时间”的数据类型,因为原始类型是TimeSpan,随后更改为字符串:

[System.Xml.Serialization.XmlElementAttribute(DataType="time", Order=3)]

然而,实际的类型是字符串

public string TimeProperty {
    get {
        return this.timePropertyField;
    }
    set {
        this.timePropertyField = value;
        this.RaisePropertyChanged("TimeProperty");
    }
}

通过删除DateType属性,Xml可以被序列化

[System.Xml.Serialization.XmlElementAttribute(Order=3)]
public string TimeProperty {
    get {
        return this.timePropertyField;
    }
    set {
        this.timePropertyField = value;
        this.RaisePropertyChanged("TimeProperty");
    }
}

我最近在添加一个新属性时在一个web引用部分类中得到了这个。自动生成的类添加了以下属性。

    [System.Xml.Serialization.XmlElementAttribute(Order = XX)]

我需要添加一个类似的属性,其顺序比自动生成序列中的最后一个高,这为我修复了它。

我遇到了类似的问题,结果是序列化器无法区分具有相同名称的两个类(一个是另一个的子类)。内部异常是这样的:

“BaseNamespace类型。Class1和basenamspace . subnamespace。Class1'都使用XML类型名,'Class1',来自命名空间。使用XML属性为该类型指定唯一的XML名称和/或名称空间。

BaseNamespace.SubNamespace的地方。Class1是basenamspace .Class1的一个子类。

我需要做的是添加一个属性到一个类(我添加到基类):

[XmlType("BaseNamespace.Class1")]

注意:如果你有更多的类层,你需要为它们添加一个属性。