Exemplo n.º 1
0
        public static GrouperConfiguration CreateFromAppSettings(NameValueCollection appSettings)
        {
            GrouperConfiguration config = new GrouperConfiguration();

            foreach (PropertyInfo propertyInfo in config.GetType().GetProperties())
            {
                string value = appSettings[propertyInfo.Name];
                if (value != null)
                {
                    if (propertyInfo.PropertyType == typeof(bool))
                    {
                        propertyInfo.SetValue(config, bool.Parse(value));
                    }
                    else if (propertyInfo.PropertyType == typeof(double))
                    {
                        propertyInfo.SetValue(config, double.Parse(value, System.Globalization.CultureInfo.InvariantCulture));
                    }
                    else if (propertyInfo.PropertyType == typeof(Role[]))
                    {
                        propertyInfo.SetValue(config,
                                              value.Split(',').Select(t => (Role)Enum.Parse(typeof(Role), t.Trim())).ToArray()
                                              );
                    }
                    else
                    {
                        propertyInfo.SetValue(config, value);
                    }
                }
            }
            return(config);
        }
Exemplo n.º 2
0
        public static GrouperConfiguration CreateFromHashtable(Hashtable hashtable)
        {
            GrouperConfiguration config = new GrouperConfiguration();

            foreach (PropertyInfo propertyInfo in config.GetType().GetProperties())
            {
                if (hashtable.ContainsKey(propertyInfo.Name))
                {
                    object value = hashtable[propertyInfo.Name];
                    if (propertyInfo.PropertyType == typeof(Role[]))
                    {
                        value = ((string[])value).Select(str => (Role)Enum.Parse(typeof(Role), str)).ToArray();
                    }
                    propertyInfo.SetValue(config, value);
                }
            }
            return(config);
        }