Esempio n. 1
0
        public DictionaryConvertible InjectConfiguredSettings(DictionaryConvertible instance)
        {
            Type[] types = new Type[1];
            types[0] = typeof(string);

            if (instance != null)
            {
                // use reflection to iterate over each public property of instance except for 'Problems'
                // which is reserved for error messages on the object
                var properties = settingPropertyProvider.GetSettingsProperties(instance);

                foreach (PropertyInfo property in properties)
                {
                    Type   t            = property.PropertyType;
                    string propertyName = namingStrategy.GetKeyFor(instance.GetType(), property);

                    if (ConfigurationManager.AppSettings.AllKeys.Contains(propertyName))
                    {
                        object value = ConfigurationManager.AppSettings[propertyName] as object;
                        try
                        {
                            if (t.IsEnum)
                            {
                                value = EnumHelper.ParseAs((string)value, t, true);
                            }

                            if (t == typeof(bool))
                            {
                                bool temp;
                                if (bool.TryParse((string)value, out temp))
                                {
                                    value = temp;
                                }
                            }

                            property.SetValue(instance, value, null);
                        }
                        catch (Exception setvalueEx)
                        {
                            instance.AddProblem(setvalueEx);
                        }
                    }
                }
            }

            else
            {
                throw new ArgumentNullException("Instance cannot be null!");
            }

            return(instance);
        }
        public DictionaryConvertible InjectConfiguredSettings( DictionaryConvertible instance )
        {
            Type[] types = new Type[1];
            types[0] = typeof( string );

            if ( instance != null )
            {
                // use reflection to iterate over each public property of instance except for 'Problems'
                // which is reserved for error messages on the object
                var properties = settingPropertyProvider.GetSettingsProperties( instance );

                foreach ( PropertyInfo property in properties )
                {
                    Type t = property.PropertyType;
                    string propertyName = namingStrategy.GetKeyFor( instance.GetType(), property );

                    if ( ConfigurationManager.AppSettings.AllKeys.Contains( propertyName ) )
                    {
                        object value = ConfigurationManager.AppSettings[propertyName] as object;
                        try
                        {
                            if ( t.IsEnum )
                            {
                                value = EnumHelper.ParseAs( (string)value, t, true );
                            }

                            if ( t == typeof( bool ) )
                            {
                                bool temp;
                                if ( bool.TryParse( (string)value, out temp ) )
                                {
                                    value = temp;
                                }
                            }

                            property.SetValue( instance, value, null );
                        }
                        catch ( Exception setvalueEx )
                        {
                            instance.AddProblem( setvalueEx );
                        }
                    }
                }
            }

            else
            {
                throw new ArgumentNullException( "Instance cannot be null!" );
            }

            return instance;
        }
        public IEnumerable<PropertyInfo> GetSettingsProperties( DictionaryConvertible instance )
        {
            if ( instance != null )
            {
                // use reflection to iterate over each public property of instance except for 'Problems'
                // which is reserved for error messages on the object
                return instance.GetType().GetProperties()
                                            .Where( prop => ( prop.Name.Equals( "Problems" ) == false ) )
                                            .Where( prop => prop.GetCustomAttributes( typeof( NotConfigurableAttribute ), false ).Count() == 0 );
            }

            return Enumerable.Empty<PropertyInfo>();
        }
Esempio n. 4
0
        public IEnumerable <PropertyInfo> GetSettingsProperties(DictionaryConvertible instance)
        {
            if (instance != null)
            {
                // use reflection to iterate over each public property of instance except for 'Problems'
                // which is reserved for error messages on the object
                return(instance.GetType().GetProperties()
                       .Where(prop => (prop.Name.Equals("Problems") == false))
                       .Where(prop => prop.GetCustomAttributes(typeof(NotConfigurableAttribute), false).Count() == 0));
            }

            return(Enumerable.Empty <PropertyInfo>());
        }