【问题标题】:How to serialize a list of generic type which inherit from a non generic class如何序列化从非泛型类继承的泛型类型列表
【发布时间】:2020-10-08 09:40:14
【问题描述】:

我的类结构如下:

[Serializable]
public abstract class TagConfiguration
{
    public abstract string Name { get; set; }

    // Other abstract properties

    public abstract object GetInitialValue();
    public abstract void SetInitialValue(object value);

    protected TagConfiguration() { }

    // Other methods not useful in the scope of this problem
}

[Serializable]
public class GenericTagConfiguration<T> : TagConfiguration
{
    public override string Name { get; set; }

    // Other abstract properties

    private T _initialValue;
    public T InitialValue
        {
            get => (T)GetInitialValue();
            set => SetInitialValue(value);
        }
        public override object GetInitialValue()
        {
            return _initialValue;
        }
        public override void SetInitialValue(object value)
        {
            _initialValue = (T)value;
        }

    public GenericTagConfiguration() : base() { }

    // Other methods not useful in the scope of this problem
}

这个类,顾名思义是用于配置一个标签对象,如下所示:

[Serializable]
public abstract class Tag
{
    public abstract object GetConfiguration();
    public abstract void SetConfiguration(object value);
    public abstract object GetCurrentValue();
    public abstract void SetCurrentValue(object value);

    protected Tag() { }

    // Some abstract methods
}

[Serializable]
public class GenericTag<T> : Tag
{
    private GenericTagConfiguration<T> _configuration;
    public TagConfiguration Configuration
    {
        get => (GenericTagConfiguration<T>)GetConfiguration();
        set => SetConfiguration(value);
    }
    public override object GetConfiguration()
    {
        return _configuration;
    }
    public override void SetConfiguration(object value)
    {
        _configuration = (GenericTagConfiguration<T>)value;
    }

    private T _currentValue;
    public T CurrentValue
    {
        get => (T)GetCurrentValue();
        set => SetCurrentValue(value);
    }
    public override object GetCurrentValue()
    {
        return _currentValue;
    }
    public override void SetCurrentValue(object value)
    {
        _currentValue = (T)value;
    }

    public GenericTag() : base() { }

    // The same methods from Tag overridden
}

我必须能够将List&lt;TagConfiguration&gt; 导出到 XML 文件,但我遇到的问题是每当我尝试这样做时,我都会收到一条错误消息:

Unhandled Exception: System.InvalidOperationException: There was an error generating the XML document. 
---> System.InvalidOperationException:The type GenericTagConfiguration[[System.Int32]] was not expected. 
Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

我正在使用以下代码尝试导出:

List<TagConfiguration> lstTagConf = new List<TagConfiguration>
{
    tagConf,
    tagConf2
};

XmlSerializer xs = new XmlSerializer(typeof(List<TagConfiguration>));
using (TextWriter tw = new StreamWriter(@"C:\Users\Administrator\Documents\listTag.xml"))
{
    xs.Serialize(tw, lstTagConf);
}

问题

我添加Tag 类的原因是我需要能够导入和导出List&lt;Tag&gt; 以及List&lt;TagConfiguration&gt;。

我需要写一个接口来做这个吗?

【问题讨论】:

  • Xml 序列化当你有一个基类和一个继承类时你需要使用 XmlInclude。请参阅:docs.microsoft.com/en-us/dotnet/api/…
  • 我明白这一点,但我将如何包含正确类型的 GenericTagConfiguration ?

标签: c# xml list generics serialization


【解决方案1】:

我找到的答案(感谢jdweng 和this question)是在TagConfiguration 类之前添加以下内容:

[XmlInclude(typeof(GenericTagConfiguration<int>)), ...] // as many different T as you would use
[Serializable]
public abstract class TagConfiguration
{
    // Same data
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 2015-09-15
    • 2019-07-11
    • 1970-01-01
    相关资源
    最近更新 更多