internal static void SetAttributes(IParseSupport o, Dictionary<string, string> attributes) { Type dataType; object val; // Preparse attributes attributes = o.ParseSpecial(attributes); IDictionary setters = GetSetters(o.GetType()); MethodInfo setter; foreach (var name in attributes.Keys) { if (name == "event") continue; if (name == "source") setter = (MethodInfo) setters["src"]; else setter = (MethodInfo) setters[stripIllegalCharacters(name)]; if (setter == null) { // No setter found to key, try general parser if (!o.Parse(name, attributes[name])) { #if LOGGER logger.Error("Unable to set property '" + name + "' on " + o.GetType() + ": no setter"); #endif throw new ManagerException("Parse error key '" + name + "' on " + o.GetType()); } } else { dataType = (setter.GetParameters()[0]).ParameterType; if (dataType == typeof (bool)) val = IsTrue(attributes[name]); else if (dataType == typeof (string)) val = ParseString(attributes[name]); else if (dataType == typeof (Int32)) { Int32 v = 0; Int32.TryParse(attributes[name], out v); val = v; } else if (dataType == typeof (Int64)) { Int64 v = 0; Int64.TryParse(attributes[name], out v); val = v; } else if (dataType == typeof (double)) { Double v = 0.0; Double.TryParse(attributes[name], NumberStyles.AllowDecimalPoint, Common.CultureInfoEn, out v); val = v; } else if (dataType == typeof (decimal)) { Decimal v = 0; Decimal.TryParse(attributes[name], NumberStyles.AllowDecimalPoint, Common.CultureInfoEn, out v); val = v; } else if (dataType.IsEnum) { try { val = Convert.ChangeType(Enum.Parse(dataType, attributes[name], true), dataType); } catch (Exception ex) { #if LOGGER logger.Error( "Unable to convert value '" + attributes[name] + "' of property '" + name + "' on " + o.GetType() + " to required enum type " + dataType, ex); continue; #else throw new ManagerException("Unable to convert value '" + attributes[name] + "' of property '" + name + "' on " + o.GetType() + " to required enum type " + dataType, ex); #endif } } else { try { ConstructorInfo constructor = dataType.GetConstructor(new[] {typeof (string)}); val = constructor.Invoke(new object[] {attributes[name]}); } catch (Exception ex) { #if LOGGER logger.Error( "Unable to convert value '" + attributes[name] + "' of property '" + name + "' on " + o.GetType() + " to required type " + dataType, ex); continue; #else throw new ManagerException("Unable to convert value '" + attributes[name] + "' of property '" + name + "' on " + o.GetType() + " to required type " + dataType, ex); #endif } } try { setter.Invoke(o, new[] {val}); } catch (Exception ex) { #if LOGGER logger.Error("Unable to set property '" + name + "' on " + o.GetType(), ex); #else throw new ManagerException("Unable to set property '" + name + "' on " + o.GetType(), ex); #endif } } } }
internal static void SetAttributes(IParseSupport o, Dictionary <string, string> attributes) { Type dataType; object val; // Preparse attributes attributes = o.ParseSpecial(attributes); IDictionary setters = Helper.GetSetters(o.GetType()); MethodInfo setter; foreach (string name in attributes.Keys) { if (name == "event") { continue; } if (name == "source") { setter = (MethodInfo)setters["src"]; } else { setter = (MethodInfo)setters[stripIllegalCharacters(name)]; } if (setter == null) { // No setter found to key, try general parser if (!o.Parse(name, (string)attributes[name])) { #if LOGGER logger.Error("Unable to set property '" + name + "' on " + o.GetType() + ": no setter"); #endif throw new ManagerException("Parse error key '" + name + "' on " + o.GetType()); } } else { dataType = (setter.GetParameters()[0]).ParameterType; if (dataType == typeof(bool)) { val = Helper.IsTrue((string)attributes[name]); } else if (dataType == typeof(string)) { val = Helper.ParseString((string)attributes[name]); } else if (dataType == typeof(Int32)) { Int32 v = 0; Int32.TryParse((string)attributes[name], out v); val = v; } else if (dataType == typeof(Int64)) { Int64 v = 0; Int64.TryParse((string)attributes[name], out v); val = v; } else if (dataType == typeof(double)) { Double v = 0.0; Double.TryParse((string)attributes[name], System.Globalization.NumberStyles.AllowDecimalPoint, Common.CultureInfoEn, out v); val = v; } else if (dataType == typeof(decimal)) { Decimal v = 0; Decimal.TryParse((string)attributes[name], System.Globalization.NumberStyles.AllowDecimalPoint, Common.CultureInfoEn, out v); val = v; } else if (dataType.IsEnum) { try { val = Convert.ChangeType(Enum.Parse(dataType, (string)attributes[name], true), dataType); } catch (Exception ex) { #if LOGGER logger.Error("Unable to convert value '" + attributes[name] + "' of property '" + name + "' on " + o.GetType() + " to required enum type " + dataType, ex); continue; #else throw new ManagerException("Unable to convert value '" + attributes[name] + "' of property '" + name + "' on " + o.GetType() + " to required enum type " + dataType, ex); #endif } } else { try { ConstructorInfo constructor = dataType.GetConstructor(new Type[] { typeof(string) }); val = constructor.Invoke(new object[] { attributes[name] }); } catch (Exception ex) { #if LOGGER logger.Error("Unable to convert value '" + attributes[name] + "' of property '" + name + "' on " + o.GetType() + " to required type " + dataType, ex); continue; #else throw new ManagerException("Unable to convert value '" + attributes[name] + "' of property '" + name + "' on " + o.GetType() + " to required type " + dataType, ex); #endif } } try { setter.Invoke(o, new object[] { val }); } catch (Exception ex) { #if LOGGER logger.Error("Unable to set property '" + name + "' on " + o.GetType(), ex); continue; #else throw new ManagerException("Unable to set property '" + name + "' on " + o.GetType(), ex); #endif } } } }