自定义序列化对象c#.doc_第1页
自定义序列化对象c#.doc_第2页
自定义序列化对象c#.doc_第3页
自定义序列化对象c#.doc_第4页
自定义序列化对象c#.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

自定义序列化对象很多时候,我们需要将对象序列化成字符串保存到内存、磁盘或者 Page.ViewState 中。基于种种原因,我们希望序列化结果尽可能小,尽可能简单,即便用其他的方法(比如正则表达式)也能解析出数据。BinaryFormatter 的结果转换成字符串(或者Base64)长度太大,而 XmlSerializer 对数据类型支持有限,显然内置的序列化引擎不足以满足我们的需求,还是自己丰衣足食。下面的代码可能还不完善,仅供参考,内容比较简单,不做详述。/ / 序列化/ public static string SerializeObject(object o)char sep1 = |;char sep2 = ,;char sep3 = =;StringBuilder sb = new StringBuilder();FieldInfo fields = o.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);foreach (FieldInfo field in fields)object value = field.GetValue(o);if (value != null)if (field.FieldType.GetInterface(IDictionary) != null)foreach (object key in (value as IDictionary).Keys)sb.AppendFormat(0312, key, (value as IDictionary)key, sep2, sep3);if (sbsb.Length - 1 = sep2) sb.Remove(sb.Length - 1, 1);else if (field.FieldType.GetInterface(IList) != null)foreach (object v in (value as IList)sb.AppendFormat(01, v, sep2);if (sbsb.Length - 1 = sep2) sb.Remove(sb.Length - 1, 1);else if (field.FieldType = typeof(Boolean)sb.Append(bool)value ? T : );elsesb.Append(value);sb.Append(sep1);if (sbsb.Length - 1 = sep1) sb.Remove(sb.Length - 1, 1);return sb.ToString();/ / 反序列化/ public static T DeserializeObject(string s)where T : new()char sep1 = |;char sep2 = ,;char sep3 = =;T o = new T();FieldInfo fields = o.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);string values = s.Split(sep1);for (int i = 0; i fields.Length; i+)FieldInfo field = fieldsi;if (String.IsNullOrEmpty(valuesi) continue;if (field.FieldType.GetInterface(IDictionary) != null)string vs = valuesi.Split(sep2);IDictionary dictionary = field.GetValue(o) as IDictionary;Type key = field.FieldType.IsGenericType ? field.FieldType.GetGenericArguments()0 : typeof(Object);Type value = field.FieldType.IsGenericType ? field.FieldType.GetGenericArguments()1 : typeof(Object);if (dictionary = null)dictionary = (IDictionary)Activator.CreateInstance(field.FieldType);field.SetValue(o, dictionary);foreach (string v in vs)string ns = v.Split(sep3);dictionary.Add(Convert.ChangeType(ns0, key), Convert.ChangeType(ns1, value);else if (field.FieldType.GetInterface(IList) != null)string vs = valuesi.Split(sep2);if (field.FieldType.IsArray)Type t = field.FieldType.GetElementType();Array array = Array.CreateInstance(t, vs.Length);for (int x = 0; x vs.Length; x+)array.SetValue(Convert.ChangeType(vsx, t), x);field.SetValue(o, array);elseIList list = field.GetValue(o) as IList;Type t = field.FieldType.IsGenericType ? field.FieldType.GetGenericArguments()0 : typeof(Object);if (list = null)list = (IList)Activator.CreateInstance(field.FieldType);field.SetValue(o, list);foreach (string v in vs)list.Add(Convert.ChangeType(v, t);else if (field.FieldType = typeof(Boolean)field.SetValue(o, valuesi = T ? true : false);else if (field.FieldType.IsEnum)field.SetValue(o, Enum.Parse(field.FieldType, valuesi, true);elsefield.SetValue(o, Convert.ChangeType(valuesi, field.FieldType);return o;测试代码 Serializablepublic class MyClassprivate int valueType;public int ValueTypeget return valueType; set valueType = value; private object obj;public object Objectget return obj; set obj = value; private bool boolean;public bool Booleanget return boolean; set boolean = value; private string array;public string Arrayget return array; set array = value; private List list;public List Listget return list; set list = value; private ArrayList arrayList;public ArrayList ArrayListget return arrayList; set arrayList = value; private Hashtable hashtable;public Hashtable Hashtableget return hashtable; set hashtable = value; private Dictionary dictionary;public Dictionary Dictionaryget return dictionary; set dictionary = value; class Programstatic void Main(string args)/Test();MyClass o = new MyClass();o.List = new List();o.Dictionary = new Dictionary();o.ArrayList = new ArrayList();o.Hashtable = new Hashtable();o.ValueType = 123456;o.Object = DateTime.Now;o.Boolean = true;o.Dictionary.Add(dict1, 1);o.Dictionary.Add(dict2, 2);o.Array = new string array1, array2, array3 ;o.List.Add(list1);o.List.Add(list2);o.ArrayList.Add(ArrayList1);o.ArrayList.Add(ArrayList2);o.Hashtable.Add(Hashtable1, 1);o.Hashtable.Add(Hashtable2, 2);/ SerializeObjectstring s = SerializeObject(o);Console.WriteLine(s);MyClass m = DeserializeObject(s);Console.W

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论