自定義序列化對(duì)象c#.doc_第1頁(yè)
自定義序列化對(duì)象c#.doc_第2頁(yè)
自定義序列化對(duì)象c#.doc_第3頁(yè)
自定義序列化對(duì)象c#.doc_第4頁(yè)
自定義序列化對(duì)象c#.doc_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

自定義序列化對(duì)象很多時(shí)候,我們需要將對(duì)象序列化成字符串保存到內(nèi)存、磁盤(pán)或者 Page.ViewState 中。基于種種原因,我們希望序列化結(jié)果盡可能小,盡可能簡(jiǎn)單,即便用其他的方法(比如正則表達(dá)式)也能解析出數(shù)據(jù)。BinaryFormatter 的結(jié)果轉(zhuǎn)換成字符串(或者Base64)長(zhǎng)度太大,而 XmlSerializer 對(duì)數(shù)據(jù)類(lèi)型支持有限,顯然內(nèi)置的序列化引擎不足以滿足我們的需求,還是自己豐衣足食。下面的代碼可能還不完善,僅供參考,內(nèi)容比較簡(jiǎn)單,不做詳述。/ / 序列化/ 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;測(cè)試代碼 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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論