派生类的 XML 反序列化

Nov*_*off 3 .net c# serialization

如何创建可以采用类对象或任何派生类的反序列化方法?

public class Config
{
    public string appname;
}

public class CustomConfig1 : Config
{
    public string CustomConfig1Param1;
    public string CustomConfig1Param2;
}

public class CustomConfig2 : Config
{
    public string CustomConfig2Param1;
    public string CustomConfig2Param2;
}
Run Code Online (Sandbox Code Playgroud)

我想获得类似定义输入对象类型的序列化方法的东西:

public string serialize(object obj)
{
    XmlSerializer serializer = new XmlSerializer(obj.GetType());
    StringWriter serialized = new StringWriter();
    serializer.Serialize(serialized, obj);
    return serialized.ToString();
}
Run Code Online (Sandbox Code Playgroud)

但是当我从 DB 读取 XML 时,我无法定义对象的类型,所以我无法将它传递给XmlSerializer。它可能是Config对象或任何派生类

请帮忙。如何定义输入对象的类型?

Mar*_*ell 7

[XmlInclude(typeof(CustomConfig1))]
[XmlInclude(typeof(CustomConfig2))]
public class Config
{
    public string appname;
}
Run Code Online (Sandbox Code Playgroud)

然后只需序列化/反序列化指定typeof(Config); 该库将根据数据为您提供适当类型的实例。


编辑:完整示例,包括不对子类型进行硬编码的偏好:

using System;
using System.IO;
using System.Xml.Serialization;

public class Config
{
    public string appname;
}

public class CustomConfig1 : Config
{
    public string CustomConfig1Param1;
    public string CustomConfig1Param2;
}

public class CustomConfig2 : Config
{
    public string CustomConfig2Param1;
    public string CustomConfig2Param2;
}

static class Program
{
    static void Main()
    {
        var original = new CustomConfig1
        {
            appname = "foo",
            CustomConfig1Param1 = "x",
            CustomConfig1Param2 = "y"
        };
        var xml = Serialize(original);
        var clone = DeserializeConfig(xml);
        Console.WriteLine(clone.appname);
        var typed = (CustomConfig1)clone;
        Console.WriteLine(typed.CustomConfig1Param1);
        Console.WriteLine(typed.CustomConfig1Param2);
    }
    public static string Serialize(Config obj)
    {
        using (var serialized = new StringWriter())
        {
            GetConfigSerializer().Serialize(serialized, obj);
            return serialized.ToString();
        }
    }
    public static Config DeserializeConfig(string xml)
    {
        using(var reader = new StringReader(xml))
        {
            return (Config)GetConfigSerializer().Deserialize(reader);
        }
    }
    static Type[] GetKnownTypes()
    {
        // TODO: resolve types properly
        return new[] { typeof(CustomConfig1), typeof(CustomConfig2) };
    }
    private static XmlSerializer configSerializer;
    public static XmlSerializer GetConfigSerializer()
    {
        return configSerializer ?? (configSerializer =
            new XmlSerializer(typeof(Config), GetKnownTypes()));
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 因此,基类必须为此具有派生类的知识。是否存在基类不知道派生类的解决方案,例如,当它们位于单独的程序集中时? (2认同)