[英]How to serialize a list of generic type which inherit from a non generic class
I have a class structure as follows:我有一个 class 结构如下:
[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
}
This class, as the name implies is for the configuration of a Tag object which looks like this:这个 class,顾名思义是用于配置一个标签 object,如下所示:
[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
}
I have to be able to export a List<TagConfiguration>
to an XML file but the issue I have is whenever I try to do so, I get an error saying:我必须能够将
List<TagConfiguration>
导出到 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.
I'm using the following code to attempt the export:我正在使用以下代码尝试导出:
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);
}
QUESTION问题
The reason I added the Tag
class is that I would need to be able to import and export a List<Tag>
as well as a List<TagConfiguration>
.我添加
Tag
class 的原因是我需要能够导入和导出List<Tag>
以及List<TagConfiguration>
。
Do I need to write an interface to do this?我需要编写一个接口来执行此操作吗?
The answer I found (Thanks to jdweng and this question ) was to add the following before the TagConfiguration
class:我找到的答案(感谢jdweng和这个问题)是在
TagConfiguration
class 之前添加以下内容:
[XmlInclude(typeof(GenericTagConfiguration<int>)), ...] // as many different T as you would use
[Serializable]
public abstract class TagConfiguration
{
// Same data
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.