Inheritance: ConfigurationElement
Esempio n. 1
0
        public static bool AddProperty(string propertyName, object defaultValue, Type propertyType)
        {
            bool added = false;

            if (!ContainsProperty(propertyName))
            {
                Property p = new Property(propertyName, propertyType, defaultValue);
                _propertyList.Add(propertyName, p);
                added = true;
            }

            if (!ContainsKey(propertyName))
            {
                if (IsStringCollection(propertyType))
                {
                    SettingElement se = new SettingElement(propertyName, SettingsSerializeAs.Xml);
                    SettingValueElement sve = new SettingValueElement();
                    XmlDocument doc = new XmlDocument();
                    XmlNode n = doc.CreateNode(XmlNodeType.Element, "value", String.Empty);
                    sve.ValueXml = n;
                    se.Value = sve;
                    _css.Settings.Add(se);
                    SetPropertyValue(propertyName, null);
                }
                else
                {
                    SettingElement se = new SettingElement(propertyName,SettingsSerializeAs.String);
                    SettingValueElement sve = new SettingValueElement();
                    XmlDocument doc = new XmlDocument();
                    XmlNode n = doc.CreateNode(XmlNodeType.Element, "value", String.Empty);
                    n.InnerText = (defaultValue == null ? String.Empty : defaultValue.ToString());
                    sve.ValueXml = n;
                    se.Value = sve;
                    _css.Settings.Add(se);
                }
                added = true;
            }
            
            return added;
        }
		//TODO (CR Sept 2010): instead of automatically storing all the defaults, should we 
		//just store what we're given and also delete stuff when we're given null?  Then
		//we could just return what's there in the "get" method rather than removing the defaults.
		private static ClientSettingsSection CreateDefaultSection(IEnumerable<PropertyInfo> properties)
		{
			var section = new ClientSettingsSection();
			section.SectionInformation.RequirePermission = false;

			foreach (PropertyInfo property in properties)
			{
				var element = new SettingElement(property.Name, SettingsClassMetaDataReader.GetSerializeAs(property));
				var valueElement = new SettingValueElement();
				element.Value = valueElement;
				
				string value = SettingsClassMetaDataReader.GetDefaultValue(property, false);
				SetElementValue(element, value);
				section.Settings.Add(element);
			}

			return section;
		}
Esempio n. 3
0
        public override bool Equals(object settingValue)
        {
            SettingValueElement u = settingValue as SettingValueElement;

            return(u != null && Equals(u.ValueXml, ValueXml));
        }
Esempio n. 4
0
        } // SaveAs

        // ----------------------------------------------------------------------
        public void Import(UserConfig importUserConfig, bool overwriteSettings)
        {
            if (importUserConfig == null)
            {
                throw new ArgumentNullException("importUserConfig");
            }

            foreach (ConfigurationSectionGroup importSectionGroup in importUserConfig.configuration.SectionGroups)
            {
                UserSettingsGroup importUserSettingsGroup = importSectionGroup as UserSettingsGroup;
                if (importUserSettingsGroup == null)
                {
                    continue;
                }

                UserSettingsGroup userSettingsGroup = configuration.SectionGroups[importSectionGroup.Name] as UserSettingsGroup;
                if (userSettingsGroup == null)
                {
                    userSettingsGroup = new UserSettingsGroup();
                    configuration.SectionGroups.Add(importSectionGroup.Name, userSettingsGroup);
                }

                foreach (ConfigurationSection importSection in importSectionGroup.Sections)
                {
                    ClientSettingsSection importClientSettingsSection = importSection as ClientSettingsSection;
                    if (importClientSettingsSection == null)
                    {
                        continue;
                    }

                    ClientSettingsSection clientSettingsSection = userSettingsGroup.Sections[importSection.SectionInformation.Name] as ClientSettingsSection;
                    if (clientSettingsSection == null)
                    {
                        clientSettingsSection = new ClientSettingsSection();
                        userSettingsGroup.Sections.Add(importSection.SectionInformation.Name, clientSettingsSection);
                    }

                    foreach (SettingElement importSettingElement in importClientSettingsSection.Settings)
                    {
                        bool newSetting = false;

                        SettingElement settingElement = clientSettingsSection.Settings.Get(importSettingElement.Name);
                        if (settingElement == null)
                        {
                            newSetting = true;
                            settingElement = new SettingElement();
                            settingElement.Name = importSettingElement.Name;
                            settingElement.SerializeAs = importSettingElement.SerializeAs;
                            clientSettingsSection.Settings.Add(settingElement);
                        }

                        if (!newSetting && !overwriteSettings)
                        {
                            continue;
                        }

                        SettingValueElement settingValueElement = new SettingValueElement();
                        settingValueElement.ValueXml = importSettingElement.Value.ValueXml;
                        settingElement.SerializeAs = importSettingElement.SerializeAs;
                        settingElement.Value = settingValueElement;
                        clientSettingsSection.Settings.Add(settingElement);
                    }
                }
            }
        } // Import
Esempio n. 5
0
        public override bool Equals(object settingValue)
        {
            SettingValueElement element = settingValue as SettingValueElement;

            return((element != null) && object.Equals(element.ValueXml, this.ValueXml));
        }
 public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
 {
     try
     {
         Configuration config = null;
         ClientSettingsSection clientSettings = GetUserSettings(out config, false);
         clientSettings.Settings.Clear();
         foreach (SettingsPropertyValue spv in collection)
         {
             SettingValueElement sve = new SettingValueElement();
             sve.ValueXml = SerializeToXmlElement(spv.Property, spv);
             SettingElement se = new SettingElement(spv.Name, spv.Property.SerializeAs);
             se.Value = sve;
             clientSettings.Settings.Add(se);
         }
         config.Save();
         config = null;
     }
     catch
     {
         // suppress
     }
 }