简体   繁体   English

在非序列化方法上对不可序列化对象进行XML序列化时出错

[英]Error on XML serializing on a non-serializable object on a non-serialized method

I'm using this code 我正在使用此代码

XmlSerializer xs = new XmlSerializer(typeof(T));

To serialize an object. 序列化对象。

Now I get an error on this line of code (which is in an object nested way down in the to be serialized object) 现在,我在这行代码中遇到错误(它是在要序列化的对象中向下嵌套的对象中)

public override List<BrokenRule> GetBrokenRules() {

the error is Csla.Validation.BrokenRule cannot be serialized because it does not have a parameterless constructor. 错误是Csla.Validation.BrokenRule无法序列化,因为它没有无参数构造函数。

Now that is correct, but I thought methods were not serialized, so what is the problem (the Csla.Validation.BrokenRule is not used elsewhere) 现在是正确的,但是我认为methods没有序列化,所以出了什么问题( Csla.Validation.BrokenRule在其他地方未使用)

If it is by design, this behavior, is there a way to runtime say "ignore this part of the object"? 如果是设计使然,那么是否有一种方法可以让运行时说“忽略对象的这一部分”?

Editing the objects code with attributes etc. is not allowed.... 不允许使用属性等编辑对象代码。

One way you could serialize this class to XML is to create an XmlSerializer for the derived class and pass XmlAttributeOverrides that specify that the property which returns a class is XmlIgnore . 可以将该类序列化为XML的一种方法是为派生类创建XmlSerializer ,并传递XmlAttributeOverrides ,这些XmlAttributeOverrides指定返回类的属性为XmlIgnore However, as specified in the documentation , you must cache and re-use this XmlSerializer : 但是,按照文档中的指定,您必须缓存并重新使用以下XmlSerializer

To increase performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified types. 为了提高性能,XML序列化基础结构动态生成程序集以序列化和反序列化指定的类型。 The infrastructure finds and reuses those assemblies. 基础结构查找并重用这些程序集。 This behavior occurs only when using the following constructors: 仅当使用以下构造函数时,才会发生此现象:

XmlSerializer.XmlSerializer(Type) XmlSerializer.XmlSerializer(类型)

XmlSerializer.XmlSerializer(Type, String) XmlSerializer.XmlSerializer(类型,字符串)

If you use any of the other constructors, multiple versions of the same assembly are generated and never unloaded, which results in a memory leak and poor performance. 如果使用任何其他构造函数,则将生成同一程序集的多个版本,并且永远不会将其卸载,这会导致内存泄漏和性能下降。 The easiest solution is to use one of the previously mentioned two constructors. 最简单的解决方案是使用前面提到的两个构造函数之一。 Otherwise, you must cache the assemblies in a Hashtable, as shown in the following example. 否则,您必须将程序集缓存在哈希表中,如以下示例所示。

Rather than a HashTable , in simple cases I will construct my XmlSerializer in the static constructor for the class I need to serialize. 在简单的情况下,我将在静态构造函数中为需要序列化的类构造XmlSerializer ,而不是HashTable This is fairly lazy, and is guaranteed to be thread safe. 这是相当懒惰的,并且保证是线程安全的。 Thus, for instance: 因此,例如:

public class NoParameterlessConstructor
{
    public string Value { get; set; }

    public NoParameterlessConstructor(string value)
    {
        this.Value = value;
    }
}

public class BaseClass
{
    public NoParameterlessConstructor NoParameterlessConstructor { get; set; }
}

public class DerivedClass : BaseClass
{
    static XmlSerializer serializer = null;

    static DerivedClass()
    {
        var xOver = new XmlAttributeOverrides();
        var attrs = new XmlAttributes() { XmlIgnore = true };
        xOver.Add(typeof(BaseClass), "NoParameterlessConstructor", attrs); // Must use BaseClass here not DerivedClass!

        serializer = new XmlSerializer(typeof(DerivedClass), xOver);
    }

    public static XmlSerializer DerivedClassXmlSerializer { get { return serializer; } }

    public int Id { get; set; }
}

And then, to test: 然后,进行测试:

public static class TestNoParameterlessConstructor
{
    public static void Test()
    {
        var derived = new DerivedClass { Id = 1, NoParameterlessConstructor = new NoParameterlessConstructor("Test") };
        var xml = XmlSerializationHelper.GetXml(derived, DerivedClass.DerivedClassXmlSerializer);
        Debug.WriteLine(xml);
    }
}

which outputs 哪个输出

<?xml version="1.0" encoding="utf-16"?>
<DerivedClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Id>1</Id>
</DerivedClass>

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

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