c#反序列化
C#序列化(转载)
2011-03-15 | 凯之风 | 转藏(2)
序列化有下列要求:
A> 只序列化PUBLIC的成员变量和属性
B> 类必须有默认识构造函数
C> 如果类没实现自定义序列化(2),那么将按默认方式序列化(1)
D> XmlSerializer不能用于序列化任何实现了IDictionary接口的类的实体,比方说Hashtable。但SoapFormatter和BinaryFormatter没有这个限制。 所以在XML序列化过程中要用[XmlIgnore]标记其不被序列化
1>基本序列化
通过[Serializable]标记,标识该类可序列化
[Serializable]
public class MyObject {
public int n1 = 0;
public int n2 = 0;
public String str = null;
}
1.1序列化成文件
1.1.1 二进制的序列化和反序列化
//序列化
MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "一些字符串";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
//反序列化
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open,
FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject) formatter.Deserialize(fromStream);
stream.Close();
1.1.2 XML序列化和反序列化
上面的代码换成SoapFormatter即可以生成XML
1.2选择性序列化
[Serializable]
public class MyObject
{
public int n1;
[NonSerialized]
//标记N2不序列化
public int n2;
public String str;
private int n3;
[NonSerialized]//对属性无效
[XmlIgnore]//标记该属性在XML序列化的过程中不序列化
public int N3
{
get{return n3;}
set{n3=value;}
}
}
注意:当类成员变量中出现不能被序列化的类或接口时候我们要通过
[NonSerialized] 只针对成员变量
[XmlIgnore] 针对成员变量和属性,只针对XML序列化
标记其不被序列化和反序列化
特别注意:要使某属性在XML序列化过程中不被序列化只能使用[XmlIgnore],[NonSerialized]无效
2>自定义序列化
为了解决1中序列化过程中会出现某些成员变量序列化过程中数据丢失的显现,
可采用自定义的序列化
[Serializable]
public class MyObject : ISerializable
{
public int n1;
public int n2;
public String str;
public MyObject()
{
}
protected MyObject(SerializationInfo info, StreamingContext context)
{
n1 = info.GetInt32("i");
n2 = info.GetInt32("j");
str = info.GetString("k");
}
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("i", n1);
info.AddValue("j", n2);
info.AddValue("k", str);
}
}
其派生类也必须实现上面的接口方法
[Serializable]
public class ObjectTwo : MyObject
{
public int num;
public ObjectTwo() : base()
{
}
protected ObjectTwo(SerializationInfo si, StreamingContext context) : base(si,context)
{
num = si.GetInt32("num");
}
public override void GetObjectData(SerializationInfo si, StreamingContext context)
{
base.GetObjectData(si,context);
si.AddValue("num", num);
}
}
切记要在反序列化构造函数中调用基类,否则,将永远不会调用基类上的构造函数,并且在反序列化后也无法构建完整的对象。
3>WEB 服务中的序列化
WEB 服务返回对象时候会自动把该对象进行XML序列化
因此,一个类要想在WEB 服务上传递必须可XML序列化
public class Test : WebService {
[WebMethod()]
public DateTime EchoString([XmlElement(DataType="string")]
string strval) {
return DateTime.Now;
}
//通过XmlInclude声明序列化过程中相关的类信息
[WebMethod()]
[XmlInclude(typeof(Car)), XmlInclude(typeof(Bike))]
public Vehicle Vehicle(string licenseNumber) {
if (licenseNumber == "0") {
Vehicle v = new Car();
v.licenseNumber = licenseNumber;
return v;
}
else if (licenseNumber == "1") {
Vehicle v = new Bike();
v.licenseNumber = licenseNumber;
return v;
}
else {
return null;
}
}
}
//通过XmlRoot指定序列化的ROOT描述信息
[XmlRoot("NewVehicle")]
public abstract class Vehicle {
public string licenseNumber;
public DateTime make;
}
public class Car : Vehicle {
}
public class Bike : Vehicle {
}
4>两个通用的序列化函数------来自Enterprise Lib 配置模块
public override object Serialize(object value)
{
XmlSerializer xmlSerializer = CreateXmlSerializer(value.GetType());
StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
XmlDocument doc = new XmlDocument();
try
{
xmlTextWriter.WriteStartElement("xmlSerializerSection");
xmlTextWriter.WriteAttributeString("type", value.GetType().AssemblyQualifiedName);
xmlSerializer.Serialize(xmlTextWriter, value);
xmlTextWriter.WriteEndElement();
xmlTextWriter.Flush();
doc.LoadXml(stringWriter.ToString());
}
finally
{
xmlTextWriter.Close();
stringWriter.Close();
}
return doc.DocumentElement;
}
public override object Deserialize(object section)
{
XmlNode sectionNode = (XmlNode)section;
XmlNode serializationNode = sectionNode.SelectSingleNode("//xmlSerializerSection");
if (serializationNode == null)
{
throw new ConfigurationException(SR.ExceptionNotInSerializedObj);
}
XmlAttribute typeAttribute = serializationNode.Attributes["type"];
if (typeAttribute == null)
{
throw new ConfigurationException(SR.ExceptionSerializationTypeMissing);
}
string typeName = typeAttribute.Value;
Type classType = null;
try
{
classType = Type.GetType(typeName, true);
}
catch (TypeLoadException ex)
{
throw new ConfigurationException(SR.ExceptionTypeCreateError(typeName), ex);
}
catch (FileNotFoundException ex)
{
throw new ConfigurationException(SR.ExceptionTypeCreateError(typeName), ex);
}
if (serializationNode.ChildNodes.Count == 0)
{
throw new ConfigurationException(SR.ExceptionSerializedObjectMissing);
}
XmlSerializer xs = CreateXmlSerializer(classType);
try
{
return xs.Deserialize(new XmlNodeReader(serializationNode.ChildNodes[0]));
}
catch (InvalidOperationException e)
{
string message = e.Message;
if (null != e.InnerException)
{
message = String.Concat(message, " ", e.InnerException.Message);
}
throw new ConfigurationException(message, e);
}
}