public static object Deserialize(Type Type, IniDictionary ini, IniCollectionSettings collectionSettings) { object Object; IniDictionary dict = new IniDictionary(); dict = IniFile.Combine(dict, ini); Object = DeserializeInternal("value", Type, Type.GetDefaultValue(), dict, string.Empty, true, collectionSettings); return(Object); }
public static IniDictionary Serialize(object Object, IniCollectionSettings collectionSettings) { IniDictionary ini = new IniDictionary() { { string.Empty, new IniGroup() } }; SerializeInternal("value", Object, ini, string.Empty, true, collectionSettings); return(ini); }
public override void Deserialize(object listObj, IniGroup group, string groupName, IniCollectionSettings collectionSettings, string name, IniDictionary ini, string fullname) { Type keytype = typeof(TKey); Type valuetype = typeof(TValue); IDictionary <TKey, TValue> list = (IDictionary <TKey, TValue>)listObj; if (!valuetype.IsComplexType()) { List <string> items = new List <string>(); switch (collectionSettings.Mode) { case IniCollectionMode.Normal: foreach (IniNameValue item in group) { if (item.Key.StartsWith(name + "[", StringComparison.Ordinal)) { items.Add(item.Key.Substring(name.Length + 1, item.Key.Length - (name.Length + 2))); } } break; case IniCollectionMode.IndexOnly: foreach (IniNameValue item in group) { items.Add(item.Key); } break; case IniCollectionMode.NoSquareBrackets: foreach (IniNameValue item in group) { if (item.Key.StartsWith(name, StringComparison.Ordinal)) { items.Add(item.Key.Substring(name.Length)); } } break; case IniCollectionMode.SingleLine: throw new InvalidOperationException("Cannot deserialize IDictionary<TKey, TValue> with IniCollectionMode.SingleLine!"); } switch (collectionSettings.Mode) { case IniCollectionMode.Normal: foreach (string item in items) { list.Add((TKey)keytype.ConvertFromString(item), (TValue)valuetype.ConvertFromString(group[name + "[" + item + "]"])); group.Remove(name + "[" + item + "]"); } break; case IniCollectionMode.IndexOnly: foreach (string item in items) { list.Add((TKey)keytype.ConvertFromString(item), (TValue)valuetype.ConvertFromString(group[item])); group.Remove(item); } break; case IniCollectionMode.NoSquareBrackets: foreach (string item in items) { list.Add((TKey)keytype.ConvertFromString(item), (TValue)valuetype.ConvertFromString(group[name + item])); group.Remove(name + item); } break; } } else { List <string> items = new List <string>(); switch (collectionSettings.Mode) { case IniCollectionMode.Normal: foreach (IniNameGroup item in ini) { if (item.Key.StartsWith(name + "[", StringComparison.Ordinal)) { items.Add(item.Key.Substring(name.Length + 1, item.Key.Length - (name.Length + 2))); } } break; case IniCollectionMode.IndexOnly: foreach (IniNameGroup item in ini) { if (item.Key != groupName) { items.Add(item.Key); } } break; case IniCollectionMode.NoSquareBrackets: foreach (IniNameGroup item in ini) { if (item.Key.StartsWith(name, StringComparison.Ordinal)) { items.Add(item.Key.Substring(name.Length)); } } break; case IniCollectionMode.SingleLine: throw new InvalidOperationException("Cannot deserialize IDictionary<TKey, TValue> with IniCollectionMode.SingleLine!"); } switch (collectionSettings.Mode) { case IniCollectionMode.Normal: foreach (string item in items) { list.Add((TKey)keytype.ConvertFromString(item), (TValue)DeserializeInternal("value", valuetype, valuetype.GetDefaultValue(), ini, name + "[" + item + "]", true, defaultCollectionSettings)); } break; case IniCollectionMode.IndexOnly: foreach (string item in items) { list.Add((TKey)keytype.ConvertFromString(item), (TValue)DeserializeInternal("value", valuetype, valuetype.GetDefaultValue(), ini, item, true, defaultCollectionSettings)); } break; case IniCollectionMode.NoSquareBrackets: foreach (string item in items) { list.Add((TKey)keytype.ConvertFromString(item), (TValue)DeserializeInternal("value", valuetype, valuetype.GetDefaultValue(), ini, name + item, true, defaultCollectionSettings)); } break; } } }
public override void Deserialize(object listObj, IniGroup group, string groupName, IniCollectionSettings collectionSettings, string name, IniDictionary ini, string fullname) { Type valuetype = typeof(T); IList <T> list = (IList <T>)listObj; int maxind = int.MinValue; if (!IsComplexType(valuetype)) { switch (collectionSettings.Mode) { case IniCollectionMode.Normal: foreach (IniNameValue item in group) { if (item.Key.StartsWith(name + "[", StringComparison.Ordinal)) { int key = int.Parse(item.Key.Substring(name.Length + 1, item.Key.Length - (name.Length + 2))); maxind = Math.Max(key, maxind); } } break; case IniCollectionMode.IndexOnly: foreach (IniNameValue item in group) { int key; if (int.TryParse(item.Key, out key)) { maxind = Math.Max(key, maxind); } } break; case IniCollectionMode.NoSquareBrackets: foreach (IniNameValue item in group) { if (item.Key.StartsWith(name, StringComparison.Ordinal)) { int key; if (int.TryParse(item.Key.Substring(name.Length), out key)) { maxind = Math.Max(key, maxind); } } } break; case IniCollectionMode.SingleLine: string[] items = group[name].Split(new[] { collectionSettings.Format }, StringSplitOptions.None); for (int i = 0; i < items.Length; i++) { list.Add((T)valuetype.ConvertFromString(items[i])); } group.Remove(name); break; } } else { switch (collectionSettings.Mode) { case IniCollectionMode.Normal: foreach (IniNameGroup item in ini) { if (item.Key.StartsWith(fullname + "[", StringComparison.Ordinal)) { int key = int.Parse(item.Key.Substring(fullname.Length + 1, item.Key.Length - (fullname.Length + 2))); maxind = Math.Max(key, maxind); } } break; case IniCollectionMode.IndexOnly: foreach (IniNameGroup item in ini) { int key; if (int.TryParse(item.Key, out key)) { maxind = Math.Max(key, maxind); } } break; case IniCollectionMode.NoSquareBrackets: foreach (IniNameGroup item in ini) { if (item.Key.StartsWith(fullname, StringComparison.Ordinal)) { int key = int.Parse(item.Key.Substring(fullname.Length)); maxind = Math.Max(key, maxind); } } break; case IniCollectionMode.SingleLine: throw new InvalidOperationException("Cannot deserialize type " + valuetype + " with IniCollectionMode.SingleLine!"); } } if (maxind == int.MinValue) { return; } int length = maxind + 1 - (collectionSettings.Mode == IniCollectionMode.SingleLine ? 0 : collectionSettings.StartIndex); if (!IsComplexType(valuetype)) { switch (collectionSettings.Mode) { case IniCollectionMode.Normal: for (int i = 0; i < length; i++) { if (group.ContainsKey(name + "[" + (i + collectionSettings.StartIndex).ToString() + "]")) { list.Add((T)valuetype.ConvertFromString(group[name + "[" + (i + collectionSettings.StartIndex).ToString() + "]"])); group.Remove(name + "[" + (i + collectionSettings.StartIndex).ToString() + "]"); } else { list.Add((T)valuetype.GetDefaultValue()); } } break; case IniCollectionMode.IndexOnly: for (int i = 0; i < length; i++) { if (group.ContainsKey((i + collectionSettings.StartIndex).ToString())) { list.Add((T)valuetype.ConvertFromString(group[(i + collectionSettings.StartIndex).ToString()])); group.Remove((i + collectionSettings.StartIndex).ToString()); } else { list.Add((T)valuetype.GetDefaultValue()); } } break; case IniCollectionMode.NoSquareBrackets: for (int i = 0; i < length; i++) { if (group.ContainsKey(name + (i + collectionSettings.StartIndex).ToString())) { list.Add((T)valuetype.ConvertFromString(group[name + (i + collectionSettings.StartIndex).ToString()])); group.Remove(name + (i + collectionSettings.StartIndex).ToString()); } else { list.Add((T)valuetype.GetDefaultValue()); } } break; } } else { switch (collectionSettings.Mode) { case IniCollectionMode.Normal: for (int i = 0; i < length; i++) { list.Add((T)DeserializeInternal("value", valuetype, valuetype.GetDefaultValue(), ini, fullname + "[" + (i + collectionSettings.StartIndex).ToString() + "]", true, defaultCollectionSettings)); } break; case IniCollectionMode.IndexOnly: for (int i = 0; i < length; i++) { list.Add((T)DeserializeInternal("value", valuetype, valuetype.GetDefaultValue(), ini, (i + collectionSettings.StartIndex).ToString(), true, defaultCollectionSettings)); } break; case IniCollectionMode.NoSquareBrackets: for (int i = 0; i < length; i++) { list.Add((T)DeserializeInternal("value", valuetype, valuetype.GetDefaultValue(), ini, fullname + (i + collectionSettings.StartIndex).ToString(), true, defaultCollectionSettings)); } break; } } }
public abstract void Deserialize(object listObj, IniGroup group, string groupName, IniCollectionSettings collectionSettings, string name, IniDictionary ini, string fullname);
private static void SerializeInternal(string name, object value, IniDictionary ini, string groupName, bool rootObject, IniCollectionSettings collectionSettings) { IniGroup group = ini[groupName]; if (value == null || value == DBNull.Value) { return; } if (!value.GetType().IsComplexType()) { group.Add(name, value.ConvertToString()); return; } if (value is IList) { int i = collectionSettings.StartIndex; switch (collectionSettings.Mode) { case IniCollectionMode.Normal: foreach (object item in (IList)value) { SerializeInternal(name + "[" + (i++).ToString() + "]", item, ini, groupName, false, defaultCollectionSettings); } return; case IniCollectionMode.IndexOnly: foreach (object item in (IList)value) { SerializeInternal((i++).ToString(), item, ini, groupName, false, defaultCollectionSettings); } return; case IniCollectionMode.NoSquareBrackets: foreach (object item in (IList)value) { SerializeInternal(name + (i++).ToString(), item, ini, groupName, false, defaultCollectionSettings); } return; case IniCollectionMode.SingleLine: List <string> line = new List <string>(); foreach (object item in (IList)value) { line.Add(item.ConvertToString()); } group.Add(name, string.Join(collectionSettings.Format, line.ToArray())); return; } } if (value is IDictionary) { switch (collectionSettings.Mode) { case IniCollectionMode.Normal: foreach (DictionaryEntry item in (IDictionary)value) { SerializeInternal(name + "[" + item.Key.ConvertToString() + "]", item.Value, ini, groupName, false, defaultCollectionSettings); } return; case IniCollectionMode.IndexOnly: foreach (DictionaryEntry item in (IDictionary)value) { SerializeInternal(item.Key.ConvertToString(), item.Value, ini, groupName, false, defaultCollectionSettings); } return; case IniCollectionMode.NoSquareBrackets: foreach (DictionaryEntry item in (IDictionary)value) { SerializeInternal(name + item.Key.ConvertToString(), item.Value, ini, groupName, false, defaultCollectionSettings); } return; case IniCollectionMode.SingleLine: throw new InvalidOperationException("Cannot serialize IDictionary with IniCollectionMode.SingleLine!"); } } string newgroup = groupName; if (!rootObject) { if (!string.IsNullOrEmpty(newgroup)) { newgroup += '.'; } newgroup += name; ini.Add(newgroup, new Dictionary <string, string>()); } foreach (MemberInfo member in value.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance)) { if (Attribute.GetCustomAttribute(member, typeof(IniIgnoreAttribute), true) != null) { continue; } string membername = member.Name; if (Attribute.GetCustomAttribute(member, typeof(IniNameAttribute), true) != null) { membername = ((IniNameAttribute)Attribute.GetCustomAttribute(member, typeof(IniNameAttribute), true)).Name; } object item; object defval; switch (member.MemberType) { case MemberTypes.Field: FieldInfo field = (FieldInfo)member; item = field.GetValue(value); defval = field.FieldType.GetDefaultValue(); break; case MemberTypes.Property: PropertyInfo property = (PropertyInfo)member; defval = property.PropertyType.GetDefaultValue(); if (property.GetIndexParameters().Length > 0) { continue; } MethodInfo getmethod = property.GetGetMethod(); if (getmethod == null) { continue; } item = getmethod.Invoke(value, null); break; default: continue; } DefaultValueAttribute defattr = (DefaultValueAttribute)Attribute.GetCustomAttribute(member, typeof(DefaultValueAttribute), true); if (defattr != null) { defval = defattr.Value; } if (Attribute.GetCustomAttribute(member, typeof(IniAlwaysIncludeAttribute), true) != null || !Equals(item, defval)) { IniCollectionSettings settings = defaultCollectionSettings; IniCollectionAttribute attr = (IniCollectionAttribute)Attribute.GetCustomAttribute(member, typeof(IniCollectionAttribute)); if (attr != null) { settings = attr.Settings; } SerializeInternal(membername, item, ini, newgroup, false, settings); } } }
private static object DeserializeInternal(string name, Type type, object defaultvalue, IniDictionary ini, string groupName, bool rootObject, IniCollectionSettings collectionSettings) { string fullname = groupName; if (!rootObject) { if (!string.IsNullOrEmpty(fullname)) { fullname += '.'; } fullname += name; } if (!ini.ContainsKey(groupName)) { return(defaultvalue); } Dictionary <string, string> group = ini[groupName]; if (!type.IsComplexType()) { if (group.ContainsKey(name)) { object converted = type.ConvertFromString(group[name]); group.Remove(name); if (converted != null) { return(converted); } } return(defaultvalue); } Type generictype; if (type.IsArray) { Type valuetype = type.GetElementType(); int maxind = int.MinValue; if (!IsComplexType(valuetype)) { switch (collectionSettings.Mode) { case IniCollectionMode.Normal: foreach (IniNameValue item in group) { if (item.Key.StartsWith(name + "[", StringComparison.Ordinal)) { int key = int.Parse(item.Key.Substring(name.Length + 1, item.Key.Length - (name.Length + 2))); maxind = Math.Max(key, maxind); } } break; case IniCollectionMode.IndexOnly: foreach (IniNameValue item in group) { int key; if (int.TryParse(item.Key, out key)) { maxind = Math.Max(key, maxind); } } break; case IniCollectionMode.NoSquareBrackets: foreach (IniNameValue item in group) { if (item.Key.StartsWith(name, StringComparison.Ordinal)) { int key; if (int.TryParse(item.Key.Substring(name.Length), out key)) { maxind = Math.Max(key, maxind); } } } break; case IniCollectionMode.SingleLine: string[] items = group[name].Split(new[] { collectionSettings.Format }, StringSplitOptions.None); Array _obj = Array.CreateInstance(valuetype, items.Length); for (int i = 0; i < items.Length; i++) { _obj.SetValue(valuetype.ConvertFromString(items[i]), i); } group.Remove(name); break; } } else { switch (collectionSettings.Mode) { case IniCollectionMode.Normal: foreach (IniNameGroup item in ini) { if (item.Key.StartsWith(fullname + "[", StringComparison.Ordinal)) { int key = int.Parse(item.Key.Substring(fullname.Length + 1, item.Key.Length - (fullname.Length + 2))); maxind = Math.Max(key, maxind); } } break; case IniCollectionMode.IndexOnly: foreach (IniNameGroup item in ini) { int key; if (int.TryParse(item.Key, out key)) { maxind = Math.Max(key, maxind); } } break; case IniCollectionMode.NoSquareBrackets: foreach (IniNameGroup item in ini) { if (item.Key.StartsWith(fullname, StringComparison.Ordinal)) { int key = int.Parse(item.Key.Substring(fullname.Length)); maxind = Math.Max(key, maxind); } } break; case IniCollectionMode.SingleLine: throw new InvalidOperationException("Cannot deserialize type " + valuetype + " with IniCollectionMode.SingleLine!"); } } if (maxind == int.MinValue) { return(Array.CreateInstance(valuetype, 0)); } int length = maxind + 1 - (collectionSettings.Mode == IniCollectionMode.SingleLine ? 0 : collectionSettings.StartIndex); Array obj = Array.CreateInstance(valuetype, length); if (!IsComplexType(valuetype)) { switch (collectionSettings.Mode) { case IniCollectionMode.Normal: for (int i = 0; i < length; i++) { if (group.ContainsKey(name + "[" + (i + collectionSettings.StartIndex).ToString() + "]")) { obj.SetValue(valuetype.ConvertFromString(group[name + "[" + (i + collectionSettings.StartIndex).ToString() + "]"]), i); group.Remove(name + "[" + (i + collectionSettings.StartIndex).ToString() + "]"); } else { obj.SetValue(valuetype.GetDefaultValue(), i); } } break; case IniCollectionMode.IndexOnly: for (int i = 0; i < length; i++) { if (group.ContainsKey((i + collectionSettings.StartIndex).ToString())) { obj.SetValue(valuetype.ConvertFromString(group[(i + collectionSettings.StartIndex).ToString()]), i); group.Remove((i + collectionSettings.StartIndex).ToString()); } else { obj.SetValue(valuetype.GetDefaultValue(), i); } } break; case IniCollectionMode.NoSquareBrackets: for (int i = 0; i < length; i++) { if (group.ContainsKey(name + (i + collectionSettings.StartIndex).ToString())) { obj.SetValue(valuetype.ConvertFromString(group[name + (i + collectionSettings.StartIndex).ToString()]), i); group.Remove(name + (i + collectionSettings.StartIndex).ToString()); } else { obj.SetValue(valuetype.GetDefaultValue(), i); } } break; } } else { switch (collectionSettings.Mode) { case IniCollectionMode.Normal: for (int i = 0; i < length; i++) { obj.SetValue(DeserializeInternal("value", valuetype, valuetype.GetDefaultValue(), ini, fullname + "[" + (i + collectionSettings.StartIndex).ToString() + "]", true, defaultCollectionSettings), i); } break; case IniCollectionMode.IndexOnly: for (int i = 0; i < length; i++) { obj.SetValue(DeserializeInternal("value", valuetype, valuetype.GetDefaultValue(), ini, (i + collectionSettings.StartIndex).ToString(), true, defaultCollectionSettings), i); } break; case IniCollectionMode.NoSquareBrackets: for (int i = 0; i < length; i++) { obj.SetValue(DeserializeInternal("value", valuetype, valuetype.GetDefaultValue(), ini, fullname + (i + collectionSettings.StartIndex).ToString(), true, defaultCollectionSettings), i); } break; } } return(obj); } if (ImplementsGenericDefinition(type, typeof(IList <>), out generictype)) { object obj = Activator.CreateInstance(type); Type valuetype = generictype.GetGenericArguments()[0]; CollectionDeserializer deserializer = (CollectionDeserializer)Activator.CreateInstance(typeof(ListDeserializer <>).MakeGenericType(valuetype)); deserializer.Deserialize(obj, group, groupName, collectionSettings, name, ini, fullname); return(obj); } if (type.ImplementsGenericDefinition(typeof(IDictionary <,>), out generictype)) { object obj = Activator.CreateInstance(type); Type keytype = generictype.GetGenericArguments()[0]; Type valuetype = generictype.GetGenericArguments()[1]; if (keytype.IsComplexType()) { return(obj); } CollectionDeserializer deserializer = (CollectionDeserializer)Activator.CreateInstance(typeof(DictionaryDeserializer <,>).MakeGenericType(keytype, valuetype)); deserializer.Deserialize(obj, group, groupName, collectionSettings, name, ini, fullname); return(obj); } object result = Activator.CreateInstance(type); MemberInfo collection = null; foreach (MemberInfo member in type.GetMembers(BindingFlags.Public | BindingFlags.Instance)) { if (Attribute.GetCustomAttribute(member, typeof(IniIgnoreAttribute), true) != null) { continue; } string membername = member.Name; if (Attribute.GetCustomAttribute(member, typeof(IniNameAttribute), true) != null) { membername = ((IniNameAttribute)Attribute.GetCustomAttribute(member, typeof(IniNameAttribute), true)).Name; } IniCollectionSettings colset = defaultCollectionSettings; IniCollectionAttribute colattr = (IniCollectionAttribute)Attribute.GetCustomAttribute(member, typeof(IniCollectionAttribute), true); if (colattr != null) { colset = colattr.Settings; } switch (member.MemberType) { case MemberTypes.Field: FieldInfo field = (FieldInfo)member; if (colset.Mode == IniCollectionMode.IndexOnly && typeof(ICollection).IsAssignableFrom(field.FieldType)) { if (collection != null) { throw new Exception("IniCollectionMode.IndexOnly cannot be used on multiple members of a Type."); } collection = member; continue; } object defval = field.FieldType.GetDefaultValue(); DefaultValueAttribute defattr = (DefaultValueAttribute)Attribute.GetCustomAttribute(member, typeof(DefaultValueAttribute), true); if (defattr != null) { defval = defattr.Value; } field.SetValue(result, DeserializeInternal(membername, field.FieldType, defval, ini, fullname, false, colset)); break; case MemberTypes.Property: PropertyInfo property = (PropertyInfo)member; if (property.GetIndexParameters().Length > 0) { continue; } if (colset.Mode == IniCollectionMode.IndexOnly && typeof(ICollection).IsAssignableFrom(property.PropertyType)) { if (collection != null) { throw new Exception("IniCollectionMode.IndexOnly cannot be used on multiple members of a Type."); } collection = member; continue; } defval = property.PropertyType.GetDefaultValue(); defattr = (DefaultValueAttribute)Attribute.GetCustomAttribute(member, typeof(DefaultValueAttribute), true); if (defattr != null) { defval = defattr.Value; } object propval = DeserializeInternal(membername, property.PropertyType, defval, ini, fullname, false, colset); MethodInfo setmethod = property.GetSetMethod(); if (setmethod == null) { continue; } setmethod.Invoke(result, new object[] { propval }); break; } } if (collection != null) { switch (collection.MemberType) { case MemberTypes.Field: FieldInfo field = (FieldInfo)collection; field.SetValue(result, DeserializeInternal(collection.Name, field.FieldType, field.FieldType.GetDefaultValue(), ini, fullname, false, ((IniCollectionAttribute)Attribute.GetCustomAttribute(collection, typeof(IniCollectionAttribute), true)).Settings)); break; case MemberTypes.Property: PropertyInfo property = (PropertyInfo)collection; object propval = DeserializeInternal(collection.Name, property.PropertyType, property.PropertyType.GetDefaultValue(), ini, fullname, false, ((IniCollectionAttribute)Attribute.GetCustomAttribute(collection, typeof(IniCollectionAttribute), true)).Settings); MethodInfo setmethod = property.GetSetMethod(); if (setmethod == null) { break; } setmethod.Invoke(result, new object[] { propval }); break; } } ini.Remove(rootObject ? string.Empty : name); return(result); }
public static void Serialize(object Object, IniCollectionSettings collectionSettings, string filename) { IniFile.Save(Serialize(Object, collectionSettings), filename); }
public static T Deserialize <T>(IniDictionary INI, IniCollectionSettings collectionSettings) { return((T)Deserialize(typeof(T), INI, collectionSettings)); }
public static object Deserialize(Type Type, string filename, IniCollectionSettings collectionSettings) { return(Deserialize(Type, IniFile.Load(filename), collectionSettings)); }
public static T Deserialize <T>(string filename, IniCollectionSettings CollectionSettings) { return(Deserialize <T>(IniFile.Load(filename), CollectionSettings)); }
public IniCollectionAttribute(IniCollectionMode mode) { Settings = new IniCollectionSettings(mode); }