比如添加一个成员:)[...] class My { ... public string SpecialMember {get; set;} }
我贴出一段代码帮助我阐述问题. using System; using System.IO; using System.Collections.Generic; using System.Xml.Serialization;public class Program { // 这个是我准备序列化的类 public class Element { // 我想特殊标记成员变量id,在反序列化的时候能够发现该成员变量是被特殊标记了的 public int id; public Element(int id) { this.id = id; } public Element() { } } // 这个类用来执行序列化和反序列化 public class Instance { private string fileName; private bool canExecute; public bool CanExecute { get { return canExecute; } } public Instance(string fileName) { this.fileName = fileName; FileInfo fileInfo = new FileInfo(fileName); if (fileInfo.Exists) canExecute = true; else canExecute = false; } public void Serialize(List<Element> list) { XmlSerializer sz = new XmlSerializer(typeof(List<Element>)); TextWriter writer = new StreamWriter(fileName); sz.Serialize(writer, list); writer.Close(); } public List<Element> Deserialize() { XmlSerializer sz = new XmlSerializer(typeof(List<Element>)); TextReader reader = new StreamReader(fileName); List<Element> list = (List<Element>)sz.Deserialize(reader); reader.Close(); return list; } } public static void Main() { // 若要运行程序,请在可执行文件所在目录新建"ig.xml" Instance instance = new Instance("ig.xml"); if (!instance.CanExecute) Console.WriteLine("ig.xml 不可操作!"); else { // 序列化 List<Element> list = new List<Element>(); for (int i = 0; i < 10; ++i) { list.Add(new Element(i)); } instance.Serialize(list); Console.WriteLine("序列化完毕!"); // 反序列化 // 采取什么方法,能够发现成员变量id是被做了特殊标记, // 假设成员变量id已在类Element做了某种标记 List<Element> anotherList = instance.Deserialize(); foreach (Element element in anotherList) { Console.WriteLine(element.id); } Console.WriteLine("反序列化完毕!"); } Console.ReadKey(); } }
class My
{
...
public string SpecialMember {get; set;}
}
using System;
using System.IO;
using System.Collections.Generic;
using System.Xml.Serialization;public class Program
{
// 这个是我准备序列化的类
public class Element
{
// 我想特殊标记成员变量id,在反序列化的时候能够发现该成员变量是被特殊标记了的
public int id; public Element(int id)
{
this.id = id;
} public Element()
{
}
} // 这个类用来执行序列化和反序列化
public class Instance
{
private string fileName; private bool canExecute;
public bool CanExecute
{
get
{
return canExecute;
}
} public Instance(string fileName)
{
this.fileName = fileName;
FileInfo fileInfo = new FileInfo(fileName);
if (fileInfo.Exists)
canExecute = true;
else
canExecute = false;
} public void Serialize(List<Element> list)
{
XmlSerializer sz = new XmlSerializer(typeof(List<Element>));
TextWriter writer = new StreamWriter(fileName);
sz.Serialize(writer, list);
writer.Close();
} public List<Element> Deserialize()
{
XmlSerializer sz = new XmlSerializer(typeof(List<Element>));
TextReader reader = new StreamReader(fileName);
List<Element> list = (List<Element>)sz.Deserialize(reader);
reader.Close(); return list;
}
} public static void Main()
{
// 若要运行程序,请在可执行文件所在目录新建"ig.xml"
Instance instance = new Instance("ig.xml");
if (!instance.CanExecute)
Console.WriteLine("ig.xml 不可操作!");
else
{
// 序列化
List<Element> list = new List<Element>();
for (int i = 0; i < 10; ++i)
{
list.Add(new Element(i));
}
instance.Serialize(list);
Console.WriteLine("序列化完毕!"); // 反序列化
// 采取什么方法,能够发现成员变量id是被做了特殊标记,
// 假设成员变量id已在类Element做了某种标记
List<Element> anotherList = instance.Deserialize();
foreach (Element element in anotherList)
{
Console.WriteLine(element.id);
}
Console.WriteLine("反序列化完毕!");
} Console.ReadKey();
}
}
当反序列化的时候,如果为Null则没被序列化,反之就序列化了。
问题解决了,贴出我的解决方法以供有共同需求的人参考.
1.自定义一个属性
2.在想要抽取的成员上标记这个属性
3.在反序列化时检索类实例的所有属性,关键在于反射,GetProperties()方法
就是这样.