コード例 #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);
        }
        private void writeSettings(IEnumerable <Type> settingTypes, TextWriter output, bool showDefaults)
        {
            var xml = new XmlTextWriter(output)
            {
                Formatting = Formatting.Indented
            };

            xml.WriteStartElement("appSettings");
            settingTypes.ToList().ForEach(t =>
            {
                // create an instance
                var settingsInstance = Activator.CreateInstance(t) as DictionaryConvertible;

                if (!showDefaults)
                {
                    // overwrite defaults with web.config/app.config values
                    settingsProvider.InjectConfiguredSettings(settingsInstance);
                }

                // then iterate over the properties
                settingPropertyProvider.GetSettingsProperties(settingsInstance)
                .ToList()
                .ForEach(p =>
                {
                    var key   = keyNamingStrategy.GetKeyFor(t, p);
                    var value = p.GetValue(settingsInstance, null);

                    xml.WriteStartElement("add");
                    xml.WriteAttributeString("key", key);
                    xml.WriteAttributeString("value", value == null ? "" : value.ToString());
                    xml.WriteEndElement();
                });
            });
            xml.WriteEndElement();
            xml.Close();
        }