简体   繁体   English

.NET XmlSerializer和C#中的嵌套类

[英].NET XmlSerializer and nested classes in C#

I have encountered some surprising behavior using XmlSerializer in C#. 我在C#中使用XmlSerializer遇到了一些令人惊讶的行为。 Consider the following piece of code. 考虑以下代码。

public class A : IEnumerable
{
    public class B
    {
        [XmlAttribute]
        public string PropA { get; set; }
        [XmlElement]
        public string PropB { get; set; }
    }

    public IEnumerator GetEnumerator ()
    {
        yield break;
    }
}

class Program
{
    static void Main (string[] args)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(A.B));

        XmlTextWriter writer = new XmlTextWriter(@"E:\temp\test.xml", Encoding.Default);
        serializer.Serialize(writer, new A.B() { PropA = "one", PropB = "two" });
    }
}

In this example I try to serialize an instance of nested class AB, which itself doesn't make use of the container class A in any way. 在这个例子中,我尝试序列化嵌套类AB的实例,它本身不以任何方式使用容器类A. But when I attempt to construct the XmlSerializer for it, the following exception is thrown: 但是当我尝试为它构造XmlSerializer时,抛出以下异常:

InvalidOperationException was unhandled: InvalidOperationException未处理:

To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. 要成为XML可序列化,从IEnumerable继承的类型必须在其继承层次结构的所有级别都具有Add(System.Object)的实现。 Test.A does not implement Add(System.Object). Test.A没有实现Add(System.Object)。

XmlSerializer is trying to apply serialization constraints against type A when I'm actually trying to serialize type AB My understanding however is that aside from privileged access to data in instances of the outer type, a nested type is not special and behaves as if it were in a namespace. 当我实际尝试序列化类型AB时,XmlSerializer正在尝试对类型A应用序列化约束我的理解是,除了在外部类型的实例中对数据的特权访问之外,嵌套类型不是特殊的并且表现得好像它是在命名空间中。

Is this understanding incorrect, and do the semantics of nested types or XmlSerializer justify this behavior, or does this feel like a bug in XmlSerializer? 这种理解是不正确的,并且嵌套类型或XmlSerializer的语义是否证明了这种行为,或者这是否像XmlSerializer中的错误一样?

In specific regard to XmlSerializer semantics, is there any documented requirement that enforces XmlSerializer constraints on all outer types when applied against a nested type? 特别是对于XmlSerializer语义,是否有任何文档要求在针对嵌套类型应用时对所有外部类型强制执行XmlSerializer约束?

http://msdn.microsoft.com/en-us/library/vstudio/ms229027%28v=vs.100%29.aspx http://msdn.microsoft.com/en-us/library/vstudio/ms229027%28v=vs.100%29.aspx

Because a nested type is treated as a member of the declaring type, the nested type has access to all other members in the declaring type. 由于嵌套类型被视为声明类型的成员,因此嵌套类型可以访问声明类型中的所有其他成员。

So if the serializer wants to work with AB, it needs the definition of A as well. 因此,如果序列化器想要使用AB,它也需要A的定义。 Where the IEnumerable validation kicks in. IEnumerable验证开始的地方。

Doesn't matter that B doesn't actually refer to anything in A :) B实际上没有引用A中的任何东西都没关系:)

it is the IEnumerable that is posing the constrains here. IEnumerable就是这里的约束。 If you add the Add method as suggested by the exception, your code will work fine. 如果您按照异常的建议添加Add方法,那么您的代码将正常工作。 Again this has little to do with XmlSerialization and more with the way IEnumerable works. 同样,这与XmlSerialization没什么关系,而且与IEnumerable的工作方式有关。 Please correct me if I am off here. 如果我在这里,请纠正我。 Check this for a good discussion on the same. 检查一下是一个很好的讨论。

The XmlSerializer gives special treatment to classes that implement IEnumerable or ICollection. XmlSerializer为实现IEnumerable或ICollection的类提供特殊处理。

more details here: XmlSerializer and IEnumerable: Serialization possible w/o parameterless constructor: Bug? 更多细节: XmlSerializer和IEnumerable:序列化可能没有参数构造函数:Bug?

Probably this is a very hard problem in the serialization run-time but i don't have any good explanation to this behavior. 可能这在序列化运行时是一个非常难的问题,但我对这种行为没有任何好的解释。 I feel like the restriction of IEnumerable don't apply to class B. 我觉得IEnumerable的限制不适用于B类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM