简体   繁体   English

如何使用通用属性序列化/反序列化类?

[英]How to serialize/deserialize a class with a generic property?

I'm unable to deserialise a class containing a generic property T. The exception I get is: 我无法反序列化包含泛型属性T的类。我得到的异常是:

Unable to generate a temporary class (result=1). 无法生成临时类(result = 1)。 error CS0030: Cannot convert type 'Teacher' to 'Programmer' error CS0029: Cannot implicitly convert type 'Programmer' to 'Teacher' 错误CS0030:无法将类型'Teacher'转换为'Programmer'错误CS0029:无法将类型'Programmer'隐式转换为'Teacher'

A contrived example of my class structure is: 我的班级结构的一个人为的例子是:

[Serializable]
public class Person<T> where T : Profession
{
    [XmlElement("Teacher", typeof(Teacher))]
    [XmlElement("Programmer", typeof(Programmer))]
    public T Profession { get; set; }
}

[Serializable]
[XmlInclude(typeof(Teacher))] // A Subclass
[XmlInclude(typeof(Programmer))] // A Subclass
public class Profession
{}

[Serializable]
public class Teacher : Profession
{
    [XmlElement]
    public int Salary { get; set; }
}


[Serializable]
public class Programmer : Profession
{
    [XmlElement]
    public long MassiveSalary { get; set; }
}

The structure of the XML file I want to deserialise is: 我要反序列化的XML文件的结构是:

<Person>
    <Programmer/>
</Person>

or could be ... 或者可能......

<Person>
    <Teacher />
</Person>

Essentially the same problem is described here but the solution/workaround used doesn't make sense to me. 这里描述的问题基本相同但使用的解决方案/解决方法对我来说没有意义。 In that case the workaround was to define an implicit conversion between the different types (Teacher/Programmer in this example). 在这种情况下,解决方法是定义不同类型之间的隐式转换(本例中为Teacher / Programmer)。

One final point is that if I comment out either of the professions, then I can deserialize at least one version of the file. 最后一点是,如果我注释掉任何一个职业,那么我可以反序列化该文件的至少一个版本。 For example: 例如:

[Serializable]
public class Person<T> where T : Profession
{
    // Comment out 'Programmer' and I can deserialise all files with a 'Teacher' XMLElement
    [XmlElement("Teacher", typeof(Teacher))]
    //[XmlElement("Programmer", typeof(Programmer))]

    public T Profession { get; set; }
}

Then it becomes possible to deserialise the file. 然后就可以对文件进行反序列化。 Can anyone explain whether or not it is possible to do this, or suggest a reasonable workound? 任何人都可以解释是否有可能这样做,或建议合理的工作量? Thanks. 谢谢。

Try following xml linq : 试试下面的xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq; 

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<Root>" +
                   "<Person>" +
                      "<Programmer/>" +
                      "<Teacher/>" +
                   "</Person>" +
                "</Root>";

            XElement root = XElement.Parse(xml);

            string[] persons = root.Descendants("Person").Elements().Select(x => x.Name.LocalName).ToArray();
        }
    }
}

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

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