有时你可能需要完成一个不可序列化或者需要对序列化内容进行改变的序列化过程。一个例子是由第三方组件提供者提供或者一个你不再拥有源码的组件中的一个类型。
有时你可能需要完成一个不可序列化或者需要对序列化内容进行改变的序列化过程。一个例子是由第三方组件提供者提供或者一个你不再拥有源码的组件中的 一个类型。下面的例子显示了一个不可序列化的类(查看列表6.26),Employee.这个类故意不生成一个默认构造函数而且它没有任何可写的字段或属 性。这意味着它不可使用任何我们到目前为止提到的序列化技术来序列化它。为了序列化这个类我们需要提供一个可以代表序列化类的代理。
列表6.26 不可序列化的Employee类
03 | private int employeeID; |
04 | private string firstName; |
05 | private string lastName; |
07 | public Employee( int employeeID, string firstName, string lastName) |
09 | this .employeeID = employeeID; |
10 | this .firstName = firstName; |
11 | this .lastName = lastName; |
16 | get { return employeeID; } |
19 | public string FirstName |
21 | get { return firstName; } |
24 | public string LastName |
26 | get { return lastName; } |
你需要两步来开发一个代理。第一步是定义代表序列化类型的数据契约。第二部是实现一个基于IDataContractSurrogate接口的数据契约代 理。我们将要检查三个主要的方法是GetDataContractType, GetDeserializedObject和GetObjectToSerialize方法。GetDataContractType 给DataContractSerializer返回序列化类型,GetDeserializedObject和 GetObjectToSerialize按要求执行反序列化和序列化。EmployeeSurrogate类在列表6.27中显示。
列表6.27 Employee代理类
001 | using System.Runtime.Serialization; |
006 | internal class EmployeeSurrogated |
009 | private int employeeID; |
011 | private string firstName; |
013 | private string lastName; |
015 | public EmployeeSurrogated( int employeeID, string firstName, string lastName) |
017 | this .employeeID = employeeID; |
018 | this .firstName = firstName; |
019 | this .lastName = lastName; |
022 | public int EmployeeID |
024 | get { return employeeID; } |
027 | public string FirstName |
029 | get { return firstName; } |
032 | public string LastName |
034 | get { return lastName; } |
038 | public class EmployeeSurrogate : IDataContractSurrogate |
040 | #region IDataContractSurrogate Members |
042 | public object GetCustomDataToExport(Type clrType, Type dataContractType) |
044 | return null ; //NotImplement |
047 | public object GetCustomDataToExport(System.Reflection.MemberInfo memberInfo, Type dataContractType) |
049 | return null ; //NotImplement |
052 | public Type GetDataContractType(Type type) |
054 | if ( typeof (Employee).IsAssignableFrom(type)) |
056 | return typeof (EmployeeSurrogated); |
061 | public object GetDeserializedObject( object obj, Type targetType) |
063 | if (obj is EmployeeSurrogated) |
065 | EmployeeSurrogated oldEmployee = (EmployeeSurrogated)obj; |
066 | Employee newEmployee = new Employee(oldEmployee.EmployeeID, oldEmployee.FirstName, oldEmployee.LastName); |
072 | public void GetKnownCustomDataTypes(System.Collections.ObjectModel.Collection<Type> customDataTypes) |
074 | throw new NotImplementedException(); |
077 | public object GetObjectToSerialize( object obj, Type targetType) |
081 | Employee oldEmployee = (Employee)obj; |
082 | EmployeeSurrogated newEmployee = new EmployeeSurrogated(oldEmployee.EmployeeID, oldEmployee.FirstName, oldEmployee.LastName); |
088 | public Type GetReferencedTypeOnImport( string typeName, string typeNamespace, object customData) |
092 | if (typeName.Equals( "EmployeeSurrogated" )) |
094 | return typeof (Employee); |
100 | public System.CodeDom.CodeTypeDeclaration ProcessImportedType(System.CodeDom.CodeTypeDeclaration typeDeclaration, System.CodeDom.CodeCompileUnit compileUnit) |
102 | return typeDeclaration; |
我们通过让DataContractSerializer知道代理类来将所有内容放到一起。你需要实例化DataContractSerializer并将EmployeeSurrogated类传递给构造函数,如列表6.28显示。
列表6.28 使用DataContractSerializer的Employee代理类
03 | static void TryToSerialize(Employee e) |
05 | DataContractSerializer dcs = new DataContractSerializer( typeof (Employee)); |
06 | using (StringWriter sw = new StringWriter()) |
08 | using (XmlWriter xw = XmlWriter.Create(sw)) |
12 | dcs.WriteObject(xw, e); |
14 | catch (InvalidDataContractException ex) |
16 | Console.WriteLine( "Cannot serialize without a surrogate! {0}" , ex.Message); |
22 | static string SerializeUsingSurrogate(DataContractSerializer dcs, Employee e) |
24 | using (StringWriter sw = new StringWriter()) |
26 | using (XmlWriter xw = XmlWriter.Create(sw)) |
28 | dcs.WriteObject(xw, e); |
35 | static Employee DeserializeUsingSurrogate(DataContractSerializer dcs, string employeeAsString) |
37 | using (StringReader tr = new StringReader(employeeAsString)) |
39 | using (XmlReader xr = XmlReader.Create(tr)) |
41 | return dcs.ReadObject(xr) as Employee; |
46 | static void Main( string [] args) |
48 | Employee e = new Employee(12345, "Daniel" , "Dong" ); |
52 | DataContractSerializer dcs = new DataContractSerializer( typeof (Employee), |
53 | null , int .MaxValue, false , false , new EmployeeSurrogate()); |
55 | string employeeAsString = SerializeUsingSurrogate(dcs, e); |
57 | e = DeserializeUsingSurrogate(dcs, employeeAsString); |
=======
转载自
作者:DanielWise
出处:http://www.cnblogs.com/danielWise/