我正在尝试创建一个类,它可以通过使用.NET XmlSerializer类对XML文件进行序列化和反序列化。我已经使用了保存功能(“SaveSettings”),但是加载功能并不是那么简单。
目前我不得不将类变量的引用传递给函数以使加载函数起作用(“LoadSettings2”)什么是理想的只是使用'this'关键字(“LoadSettings”)是无论如何要创建一个只需要传递文件路径的LoadSettings函数吗?
的Program.cs:
class Program
{
static void Main(string[] args)
{
ApplicationSettings appSet = new ApplicationSettings();
appSet.SourceDirectory = "here";
appSet.DestinationDirectory = "there";
appSet.SaveSettings(@"C:\Users\Connor\Desktop\Folder A\a.xml");
//appSet.LoadSettings(@"C:\Users\Connor\Desktop\Folder A\a.xml"); //Doesn't work
appSet.LoadSettings2(ref appSet, @"C:\Users\Connor\Desktop\Folder A\a.xml");
}
}
ApplicationSettings.cs:
public class ApplicationSettings
{
//Serialized
public string SourceDirectory;
public string DestinationDirectory;
//Not Serialized
public void SaveSettings(string filePath)
{
XmlSerializer XSerializer = new XmlSerializer(typeof(ApplicationSettings));
StreamWriter strWrite = new StreamWriter(filePath);
XSerializer.Serialize(strWrite, this);
strWrite.Close();
}
public void LoadSettings(string filePath)
{
XmlSerializer XSerializer = new XmlSerializer(typeof(ApplicationSettings));
StreamReader strRead = new StreamReader(filePath);
//Ideal but will not work
//this = (ApplicationSettings)XSerializer.Deserialize(strRead);
strRead.Close();
}
public void LoadSettings2(ref ApplicationSettings appSettings, string filePath)
{
XmlSerializer XSerializer = new XmlSerializer(typeof(ApplicationSettings));
StreamReader strRead = new StreamReader(filePath);
appSettings = (ApplicationSettings)XSerializer.Deserialize(strRead);
strRead.Close();
}
}
-
- 回答---
通过使用'David M'建议的成员副本来完成成员工作但是通过使用System.Reflection来完成它,这意味着没有使用变量名称。我相信只复制公共变量,需要对类进行更多测试。需要大约1.8秒才能执行10k次迭代。
public void LoadSettings(string filePath)
{
XmlSerializer XSerializer = new XmlSerializer(typeof(ApplicationSettings));
StreamReader strRead = new StreamReader(filePath);
ApplicationSettings settingsRead = (ApplicationSettings)XSerializer.Deserialize(strRead);
foreach (var field in typeof(ApplicationSettings).GetFields())
{
field.SetValue(this, field.GetValue(settingsRead));
}
strRead.Close();
}
答案 0 :(得分:2)
改为使其成为静态工厂方法:
public static ApplicationSettings LoadSettings(string filePath)
{
XmlSerializer XSerializer = new XmlSerializer(typeof(ApplicationSettings));
using (StreamReader strRead = new StreamReader(filePath))
{
var result = (ApplicationSettings)XSerializer.Deserialize(strRead);
}
return result;
}
否则,您将不得不反序列化到另一个实例,并且成员逐个会将值复制到当前实例。请注意,使用using
StreamReader
阻止您的情况会更好 - Dispose
调用将确保释放文件句柄。
答案 1 :(得分:0)
为什么不将LoadSettings()
设为static
方法,并将其称为:
ApplicationSettings applicationSettings = ApplicationSettings.LoadSettings();
答案 2 :(得分:0)
通过使用'David M'建议的成员副本来完成成员工作但是通过使用System.Reflection来完成它,这意味着没有使用变量名称。我相信只复制公共变量,需要对类进行更多测试。需要大约1.8秒才能执行10k次迭代。
public void LoadSettings(string filePath)
{
XmlSerializer XSerializer = new XmlSerializer(typeof(ApplicationSettings));
StreamReader strRead = new StreamReader(filePath);
ApplicationSettings settingsRead = (ApplicationSettings)XSerializer.Deserialize(strRead);
foreach (var field in typeof(ApplicationSettings).GetFields())
{
field.SetValue(this, field.GetValue(settingsRead));
}
strRead.Close();
}