/// <summary> /// This method will set the ini value to the supplied value or use the default if non supplied /// </summary> /// <param name="propertyValue"></param> public void UseValueOrDefault(string propertyValue) { Type valueType = ValueType; string propertyName = _attributes.Name; string defaultValue = _attributes.DefaultValue; bool defaultUsed = false; object defaultValueFromConfig = _containingIniSection.GetDefault(propertyName); if (string.IsNullOrEmpty(propertyValue)) { if (defaultValue != null && defaultValue.Trim().Length != 0) { propertyValue = defaultValue; defaultUsed = true; } else if (defaultValueFromConfig != null) { Log.DebugFormat("Default for Property {0} implemented!", propertyName); } else { if (_attributes.ExcludeIfNull) { Value = null; return; } Log.DebugFormat("Property {0} has no value or default value!", propertyName); } } // Now set the value if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { // Logic for Dictionary<,> Type type1 = valueType.GetGenericArguments()[0]; Type type2 = valueType.GetGenericArguments()[1]; //LOG.Info(String.Format("Found Dictionary<{0},{1}>", type1.Name, type2.Name)); object dictionary = Activator.CreateInstance(valueType); MethodInfo addMethodInfo = valueType.GetMethod("Add"); bool addedElements = false; IDictionary <string, string> properties = IniConfig.PropertiesForSection(_containingIniSection); foreach (string key in properties.Keys) { if (key != null && key.StartsWith(propertyName + ".")) { // What "key" do we need to store it under? string subPropertyName = key.Substring(propertyName.Length + 1); string stringValue = properties[key]; object newValue1 = null; object newValue2 = null; try { newValue1 = ConvertStringToValueType(type1, subPropertyName, _attributes.Separator); } catch (Exception ex) { Log.Warn(ex); //LOG.Error("Problem converting " + subPropertyName + " to type " + type1.FullName, e); } try { newValue2 = ConvertStringToValueType(type2, stringValue, _attributes.Separator); } catch (Exception ex) { Log.Warn(ex); //LOG.Error("Problem converting " + stringValue + " to type " + type2.FullName, e); } addMethodInfo.Invoke(dictionary, new[] { newValue1, newValue2 }); addedElements = true; } } // No need to return something that isn't filled! if (addedElements) { Value = dictionary; return; } if (defaultValueFromConfig != null) { Value = defaultValueFromConfig; return; } } else if (!string.IsNullOrEmpty(propertyValue)) { if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Nullable <>)) { // We are dealing with a generic type that is nullable valueType = Nullable.GetUnderlyingType(valueType); } object newValue; try { newValue = ConvertStringToValueType(valueType, propertyValue, _attributes.Separator); } catch (Exception ex1) { newValue = null; if (!defaultUsed) { try { Log.WarnFormat("Problem '{0}' while converting {1} to type {2} trying fallback...", ex1.Message, propertyValue, valueType.FullName); newValue = ConvertStringToValueType(valueType, defaultValue, _attributes.Separator); ContainingIniSection.IsDirty = true; Log.InfoFormat("Used default value {0} for property {1}", defaultValue, propertyName); } catch (Exception ex2) { Log.Warn("Problem converting fallback value " + defaultValue + " to type " + valueType.FullName, ex2); } } else { Log.Warn("Problem converting " + propertyValue + " to type " + valueType.FullName, ex1); } } Value = newValue; return; } // If nothing is set, we can use the default value from the config (if we habe one) if (defaultValueFromConfig != null) { Value = defaultValueFromConfig; return; } if (ValueType != typeof(string)) { try { Value = Activator.CreateInstance(ValueType); } catch (Exception) { Log.WarnFormat("Couldn't create instance of {0} for {1}, using default value.", ValueType.FullName, _attributes.Name); Value = default(ValueType); } } else { Value = default(ValueType); } }
/// <summary> /// This method will set the ini value to the supplied value or use the default if non supplied /// </summary> /// <param name="propertyValue"></param> public void UseValueOrDefault(string propertyValue) { Type valueType = ValueType; string propertyName = attributes.Name; string defaultValue = attributes.DefaultValue; bool defaultUsed = false; object defaultValueFromConfig = containingIniSection.GetDefault(propertyName); if (string.IsNullOrEmpty(propertyValue)) { if (defaultValue != null && defaultValue.Trim().Length != 0) { propertyValue = defaultValue; defaultUsed = true; } else if (defaultValueFromConfig != null) { LOG.DebugFormat("Default for Property {0} implemented!", propertyName); } else { if (attributes.ExcludeIfNull) { Value = null; return; } LOG.DebugFormat("Property {0} has no value or default value!", propertyName); } } // Now set the value if (valueType.IsGenericType && ValueType.GetGenericTypeDefinition() == typeof(List <>)) { string arraySeparator = attributes.Separator; object list = Activator.CreateInstance(ValueType); // Logic for List<> if (propertyValue == null) { if (defaultValueFromConfig != null) { Value = defaultValueFromConfig; return; } Value = list; return; } string[] arrayValues = propertyValue.Split(new string[] { arraySeparator }, StringSplitOptions.None); if (arrayValues == null || arrayValues.Length == 0) { Value = list; return; } bool addedElements = false; bool parseProblems = false; MethodInfo addMethodInfo = valueType.GetMethod("Add"); foreach (string arrayValue in arrayValues) { if (arrayValue != null && arrayValue.Length > 0) { object newValue = null; try { newValue = ConvertStringToValueType(valueType.GetGenericArguments()[0], arrayValue); } catch (Exception ex) { LOG.Warn(ex); //LOG.Error("Problem converting " + arrayValue + " to type " + fieldType.FullName, e); parseProblems = true; } if (newValue != null) { addMethodInfo.Invoke(list, new object[] { newValue }); addedElements = true; } } } // Try to fallback on a default if (!addedElements && parseProblems) { try { object fallbackValue = ConvertStringToValueType(valueType.GetGenericArguments()[0], defaultValue); addMethodInfo.Invoke(list, new object[] { fallbackValue }); Value = list; return; } catch (Exception ex) { LOG.Warn(ex); //LOG.Error("Problem converting " + defaultValue + " to type " + fieldType.FullName, e); } } Value = list; return; } else if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { // Logic for Dictionary<,> Type type1 = valueType.GetGenericArguments()[0]; Type type2 = valueType.GetGenericArguments()[1]; //LOG.Info(String.Format("Found Dictionary<{0},{1}>", type1.Name, type2.Name)); object dictionary = Activator.CreateInstance(valueType); MethodInfo addMethodInfo = valueType.GetMethod("Add"); bool addedElements = false; Dictionary <string, string> properties = IniConfig.PropertiesForSection(containingIniSection); foreach (string key in properties.Keys) { if (key != null && key.StartsWith(propertyName + ".")) { // What "key" do we need to store it under? string subPropertyName = key.Substring(propertyName.Length + 1); string stringValue = properties[key]; object newValue1 = null; object newValue2 = null; try { newValue1 = ConvertStringToValueType(type1, subPropertyName); } catch (Exception ex) { LOG.Warn(ex); //LOG.Error("Problem converting " + subPropertyName + " to type " + type1.FullName, e); } try { newValue2 = ConvertStringToValueType(type2, stringValue); } catch (Exception ex) { LOG.Warn(ex); //LOG.Error("Problem converting " + stringValue + " to type " + type2.FullName, e); } addMethodInfo.Invoke(dictionary, new object[] { newValue1, newValue2 }); addedElements = true; } } // No need to return something that isn't filled! if (addedElements) { Value = dictionary; return; } else if (defaultValueFromConfig != null) { Value = defaultValueFromConfig; return; } } if (defaultValueFromConfig != null) { Value = defaultValueFromConfig; return; } else { if (valueType.IsGenericType && valueType.GetGenericTypeDefinition().Equals(typeof(Nullable <>))) { // We are dealing with a generic type that is nullable valueType = Nullable.GetUnderlyingType(valueType); } object newValue = null; try { newValue = ConvertStringToValueType(valueType, propertyValue); } catch (Exception ex1) { newValue = null; if (!defaultUsed) { try { newValue = ConvertStringToValueType(valueType, defaultValue); } catch (Exception ex2) { LOG.Warn("Problem converting " + propertyValue + " to type " + valueType.FullName, ex2); } } else { LOG.Warn("Problem converting " + propertyValue + " to type " + valueType.FullName, ex1); } } Value = newValue; return; } }