/// <summary> /// Write the value to the text writer /// </summary> /// <param name="writer">TextWriter to write to</param> /// <param name="onlyProperties">true if we do not want the comment</param> public void Write(TextWriter writer, bool onlyProperties) { object myValue = Value; Type valueType = ValueType; if (myValue == null) { if (_attributes.ExcludeIfNull) { return; } if (_attributes.DefaultValue != null) { myValue = _attributes.DefaultValue; valueType = typeof(string); } else { myValue = _containingIniSection.GetDefault(_attributes.Name); if (myValue != null) { valueType = myValue.GetType(); } } } if (myValue == null) { if (_attributes.ExcludeIfNull) { return; } } if (!onlyProperties) { writer.WriteLine("; {0}", _attributes.Description); } if (myValue == null) { writer.WriteLine("{0}=", _attributes.Name); return; } if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { // Handle dictionaries. Type valueType1 = valueType.GetGenericArguments()[0]; Type valueType2 = valueType.GetGenericArguments()[1]; // Get the methods we need to deal with dictionaries. var keys = valueType.GetProperty("Keys").GetValue(myValue, null); var item = valueType.GetProperty("Item"); var enumerator = keys.GetType().GetMethod("GetEnumerator").Invoke(keys, null); var moveNext = enumerator.GetType().GetMethod("MoveNext"); var current = enumerator.GetType().GetProperty("Current").GetGetMethod(); // Get all the values. while ((bool)moveNext.Invoke(enumerator, null)) { var key = current.Invoke(enumerator, null); var valueObject = item.GetValue(myValue, new[] { key }); // Write to ini file! writer.WriteLine("{0}.{1}={2}", _attributes.Name, ConvertValueToString(valueType1, key, _attributes.Separator), ConvertValueToString(valueType2, valueObject, _attributes.Separator)); } } else { writer.WriteLine("{0}={1}", _attributes.Name, ConvertValueToString(valueType, myValue, _attributes.Separator)); } }
/// <summary> /// Write the value to the text writer /// </summary> /// <param name="writer">TextWriter to write to</param> /// <param name="onlyProperties">true if we do not want the comment</param> public void Write(TextWriter writer, bool onlyProperties) { object myValue = Value; Type valueType = ValueType; if (myValue == null) { if (attributes.ExcludeIfNull) { return; } if (attributes.DefaultValue != null) { myValue = attributes.DefaultValue; valueType = typeof(string); } else { myValue = containingIniSection.GetDefault(attributes.Name); if (myValue != null) { valueType = myValue.GetType(); } } } if (myValue == null) { if (attributes.ExcludeIfNull) { return; } } if (!onlyProperties) { writer.WriteLine("; {0}", attributes.Description); } if (myValue == null) { writer.Write("{0}=", attributes.Name); return; } if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(List <>)) { Type specificValueType = valueType.GetGenericArguments()[0]; writer.Write("{0}=", attributes.Name); int listCount = (int)valueType.GetProperty("Count").GetValue(myValue, null); // Loop though generic list for (int index = 0; index < listCount; index++) { object item = valueType.GetMethod("get_Item").Invoke(myValue, new object[] { index }); // Now you have an instance of the item in the generic list if (index < listCount - 1) { writer.Write("{0}" + attributes.Separator, ConvertValueToString(specificValueType, item)); } else { writer.Write("{0}", ConvertValueToString(specificValueType, item)); } } writer.WriteLine(); } else if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { // Handle dictionaries. Type valueType1 = valueType.GetGenericArguments()[0]; Type valueType2 = valueType.GetGenericArguments()[1]; // Get the methods we need to deal with dictionaries. var keys = valueType.GetProperty("Keys").GetValue(myValue, null); var item = valueType.GetProperty("Item"); var enumerator = keys.GetType().GetMethod("GetEnumerator").Invoke(keys, null); var moveNext = enumerator.GetType().GetMethod("MoveNext"); var current = enumerator.GetType().GetProperty("Current").GetGetMethod(); // Get all the values. while ((bool)moveNext.Invoke(enumerator, null)) { var key = current.Invoke(enumerator, null); var valueObject = item.GetValue(myValue, new object[] { key }); // Write to ini file! writer.WriteLine("{0}.{1}={2}", attributes.Name, ConvertValueToString(valueType1, key), ConvertValueToString(valueType2, valueObject)); } } else { writer.WriteLine("{0}={1}", attributes.Name, ConvertValueToString(valueType, myValue)); } }