byte[] PackageValueDictionary(KeyEntry keyEntry, Entry entry, Type type) { bool isGeneric = IsTheTypes(type, typeof(System.Collections.Generic.IDictionary <,>)); TreePackage package = new TreePackage(); package.Attributes.Add("IsGeneric", isGeneric); if (isGeneric) { Type[] types = GetTheTypes(type, typeof(System.Collections.Generic.IDictionary <,>)).GetGenericArguments(); package.Attributes.Add("T1", types[0].AssemblyQualifiedName); package.Attributes.Add("T2", types[1].AssemblyQualifiedName); Type typeX = GetTheTypes(type, typeof(System.Collections.Generic.Dictionary <,>)); if (typeX != null && typeX.GetGenericArguments()[0] == typeof(string)) { package.Attributes.Add("KeyIsString", true); System.Collections.Generic.IEqualityComparer <string> comparer = (System.Collections.Generic.IEqualityComparer <string>)FastWrapper.Get(entry.Value, "Comparer"); package.Attributes.Add("ComparerIgnoreCase", (comparer == StringComparer.CurrentCultureIgnoreCase || comparer == StringComparer.InvariantCultureIgnoreCase || comparer == StringComparer.OrdinalIgnoreCase)); } } int index = -1; foreach (object item in (System.Collections.IEnumerable)entry.Value) { index++; package.Add(index.ToString(), FastWrapper.Get(item, "Value")); Entry itemEntry = package.Entries[index.ToString()]; itemEntry.Attributes.Add("Key", FastWrapper.Get(item, "Key")); } return(package.Save()); }
public byte[] Save(object instance) { Type type = instance.GetType(); TreePackage package = new TreePackage(); package.Attributes.Add("Type", type.AssemblyQualifiedName); foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { if (!property.CanRead) { continue; } if (AttributeExtensions.IsDefined <NonPackageAttribute>(property) || AttributeExtensions.IsDefined <Formatting.IgnoreAttribute>(property)) { continue; } package.Add(property.Name, property.GetValue(instance, new object[0])); } foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.Instance)) { if (AttributeExtensions.IsDefined <NonPackageAttribute>(field) || AttributeExtensions.IsDefined <Formatting.IgnoreAttribute>(field)) { continue; } package.Add(field.Name, field.GetValue(instance)); } return(package.Save()); }
byte[] PackageValueNameValueCollection(KeyEntry keyEntry, Entry entry, Type type) { TreePackage package = new TreePackage(); System.Collections.Specialized.NameValueCollection col = (System.Collections.Specialized.NameValueCollection)entry.Value; for (int i = 0; i < col.Count; i++) { package.Add(i.ToString(), col.GetValues(i)); package.Entries[i.ToString()].Attributes.Add("Key", col.GetKey(i)); } return(package.Save()); }
byte[] PackageValueIEnumerable(KeyEntry keyEntry, Entry entry, Type type) { if (Version == 2) { if (entry.ArrayType == PackageArrayTypes.T_Array && entry.ValueType == PackageValueTypes.Byte) { byte[] buffer1 = entry.Value as byte[]; if (buffer1 != null) { return(buffer1); } IEnumerable <byte> buffer2 = entry.Value as IEnumerable <byte>; if (buffer2 != null) { buffer1 = LinqHelper.ToArray(buffer2); if (buffer1 == null) { return(new byte[0]); } return(buffer1); } IEnumerator <byte> buffer3 = entry.Value as IEnumerator <byte>; if (buffer3 != null) { using (buffer3) { List <byte> buffer3_x = new List <byte>(); while (buffer3.MoveNext()) { buffer3_x.Add(buffer3.Current); } return(buffer3_x.ToArray()); } } return(new byte[0]); } } TreePackage package = new TreePackage(); if (entry.ArrayType == PackageArrayTypes.T_Array || entry.ArrayType == PackageArrayTypes.T_List) { if (entry.ValueType == PackageValueTypes.Object) { Type t1 = null; if (entry.ArrayType == PackageArrayTypes.T_Array) { t1 = entry.Value.GetType().GetElementType(); } else { t1 = entry.Value.GetType(); if (t1.GetGenericArguments().Length == 0) { t1 = null; } else { t1 = t1.GetGenericArguments()[0]; } } if (t1 != null) { package.Attributes.Add("T1", t1.AssemblyQualifiedName); } } } int index = -1; if (IsTheTypes(type, typeof(System.Collections.IEnumerable))) { foreach (object item in (System.Collections.IEnumerable)entry.Value) { index++; package.Add(index.ToString(), item); } } else { System.Collections.IEnumerator enumerator = (System.Collections.IEnumerator)entry.Value; System.Collections.Generic.List <object> list = new System.Collections.Generic.List <object>(); while (enumerator.MoveNext()) { index++; package.Add(index.ToString(), enumerator.Current); list.Add(enumerator.Current); } IDisposable disp = enumerator as IDisposable; if (disp != null) { disp.Dispose(); } disp = null; enumerator = null; entry.Value = list; } return(package.Save()); }