本文介绍了我怎样才能支持XML序列化封装类中无参数的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的的XMLSerializer 连载一类我自己的列表。一类的属性是没有参数的构造函数密封类的实例,因此XML序列拒绝该类序列化。我怎样才能解决这个?我需要的属性被序列化。

I'm currently using an XMLSerializer to serialize a list of a class of my own. One of the class's properties is an instance of a sealed class that does not have a parameterless constructor, so the XML Serializer refuses to serialize the class. How can I get around this? I need that property to be serialized.

是否有某种方式为我指定的类应该被序列化?

Is there some way for me to specify how that class should be serialized?

我们想留在XML;在那里,我可以用,不会有这个问题的另一个XML序列化?

We'd like to stay with XML; is there another XML serializer that I could use that would not have this problem?

此外,我道歉,如果这是一种欺骗,但我不知道该怎么寻找。

Again, I apologize if this is a dupe, but I had no idea what to search.

为了澄清,我没有进入密封类的源。

To clarify, I don't have access to the source of the sealed class.

推荐答案

这是不可能直接做的; 的XmlSerializer 无法应付没有参数的构造函数的类。

It's not possible to do directly; XmlSerializer can't cope with classes that don't have a parameterless constructor.

我通常做的是包裹参类另一类是与XML兼容。包装类有一个参数的构造函数和一组读写性能;它有一个 FromXml 方法调用真正的类的构造函数。

What I normally do is wrap the parameterless class in another class that's compatible with XML. The wrapper class has a parameterless constructor and a set of read-write properties; it has a FromXml method that calls the real class's constructor.

[XmlIgnore]
public SomeClass SomeProperty { get; set; }

[XmlElement("SomeProperty")]
public XmlSomeClass XmlSomeProperty
{
    get { return XmlSomeClass.ToXml(SomeProperty); }
    set { SomeProperty = value.FromXml(); }
}

这篇关于我怎样才能支持XML序列化封装类中无参数的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:19