从基类序列化和反序列化派生类 [英] Serialize and DeSerialized derived classes from the base class
问题描述
我试图创建一个基类,可以从中继承基类(向派生类添加属性),并从基类中利用Load和Save方法。我发现自己写了Load and Save一遍又一遍,我想对它应用一些DRY ...
I am trying to create a base class where I can inherit from it (to add properties to the derived classes) and the utilized the Load and Save methods from the base class. I find myself writing the Load and Save over and over and I'd like to apply some DRY to it...
namespace Common
{
using System;
using System.IO;
using System.Xml.Serialization;
public abstract class ApplicationSettings
{
protected ApplicationSettings()
{
}
public static ApplicationSettings Load(string fileName)
{
if (!File.Exists(fileName))
{
return null;
}
XmlSerializer serializer = new XmlSerializer(typeof(ApplicationSettings));
using (StreamReader reader = new StreamReader(fileName))
{
ApplicationSettings param = (ApplicationSettings)serializer.Deserialize(reader);
reader.Close();
return param;
}
}
public void Save(string fileName)
{
XmlSerializer serializer = new XmlSerializer(typeof(ApplicationSettings));
using (StreamWriter writer = new StreamWriter(fileName))
{
serializer.Serialize(writer, this);
writer.Close();
}
}
}
}
给出这个抽象类,然后我想派生一个类,例如:
Given this abstract class, I then want to derive a class such as:
namespace Common
{
using System;
public class ApplicationParameters : ApplicationSettings
{
public ApplicationParameters()
{
}
public string AuthorizationCode
{
get;
set;
}
public string ReferenceNumber
{
get;
set;
}
}
}
对于派生类,我应该能够执行以下操作
For the Derived class, I should be able to do something like
ApplicationParameters参数=
ApplicationParmeters.Load( settings.xml);
ApplicationParameters parameters = ApplicationParmeters.Load("settings.xml");
但是,在上面的实现中,当我在调用Load方法时尝试将ApplicationSettings转换为ApplicationParameters类时,会发生编译器错误。
However, in the implementation above, an compiler error occurs when I attempt to cast the ApplicationSettings to the ApplicationParameters class when I call the Load method in the base class.
有没有办法做到这一点?
Is there a way to do this?
推荐答案
使顶级类通用,以便Save / Load方法可以支持多种类型:
Make the top level class generic so that the Save/Load methods can support multiple types:
public abstract class ApplicationSettings<T>
{
public static T Load(string xml){ // Implementation }
public static void Save (T obj) { // Implementation }
}
public class ApplicationParameters : ApplicationSettings<ApplicationParameters>
{
}
或者您也可以使静态方法本身通用:
Or you could just make the static methods themselves generic:
public abstract class ApplicationSettings
{
public static T Load<T>(string xml){ // implementation }
public static void Save<T>(T obj){ // implementation }
}
现在您将注意到,抽象父类的Save / Load方法被强力键入到子级,因此以下行将按预期工作:
You will now notice that the Save/Load methods from the abstract parent class are strongly typed to the child so that the following line will work as expected:
ApplicationParameters parameters = ApplicationParameters.Load("settings.xml");
或
ApplicationParameters parameters =
ApplicationSettings.Load<ApplicationParameters>("settings.xml");
取决于您使用的方法。
这篇关于从基类序列化和反序列化派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!