static void InitProperties () { SettingsPropertyCollection properties = new SettingsPropertyCollection (); ProfileSection config = (ProfileSection) WebConfigurationManager.GetSection ("system.web/profile"); RootProfilePropertySettingsCollection ps = config.PropertySettings; for (int i = 0; i < ps.GroupSettings.Count; i++) { ProfileGroupSettings pgs = ps.GroupSettings [i]; ProfilePropertySettingsCollection ppsc = pgs.PropertySettings; for (int s = 0; s < ppsc.Count; s++) { SettingsProperty settingsProperty = CreateSettingsProperty (pgs, ppsc [s]); ValidateProperty (settingsProperty, ppsc [s].ElementInformation); properties.Add (settingsProperty); } } for (int s = 0; s < ps.Count; s++) { SettingsProperty settingsProperty = CreateSettingsProperty (null, ps [s]); ValidateProperty (settingsProperty, ps [s].ElementInformation); properties.Add (settingsProperty); } if (config.Inherits.Length > 0) { Type profileType = ProfileParser.GetProfileCommonType (HttpContext.Current); if (profileType != null) { Type properiesType = profileType.BaseType; for (; ; ) { PropertyInfo [] pi = properiesType.GetProperties (BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); if (pi.Length > 0) for (int i = 0; i < pi.Length; i++) properties.Add (CreateSettingsProperty (pi [i])); if (properiesType.BaseType == null || properiesType.BaseType == typeof (ProfileBase)) break; properiesType = properiesType.BaseType; } } } properties.SetReadOnly (); lock (Profiles_SettingsPropertyCollection) { if (_properties == null) _properties = properties; } }
public void AddDuplicate () { SettingsPropertyCollection col = new SettingsPropertyCollection (); SettingsProperty test_prop = new SettingsProperty ("test_prop"); col.Add (test_prop); Assert.AreEqual (1, col.Count, "A1"); test_prop = new SettingsProperty ("test_prop"); col.Add (test_prop); Assert.AreEqual (1, col.Count, "A2"); }
public void Upgrade(SettingsContext context, SettingsPropertyCollection properties) { SettingsPropertyCollection propertys = new SettingsPropertyCollection(); SettingsPropertyCollection propertys2 = new SettingsPropertyCollection(); foreach (SettingsProperty property in properties) { if (IsRoamingSetting(property)) { propertys2.Add(property); } else { propertys.Add(property); } } if (propertys2.Count > 0) { this.Upgrade(context, propertys2, true); } if (propertys.Count > 0) { this.Upgrade(context, propertys, false); } }
void CacheValuesByProvider(SettingsProvider provider) { SettingsPropertyCollection col = new SettingsPropertyCollection(); foreach (SettingsProperty p in Properties) { if (p.Provider == provider) { col.Add(p); } } if (col.Count > 0) { SettingsPropertyValueCollection vals = provider.GetPropertyValues(Context, col); foreach (SettingsPropertyValue prop in vals) { if (PropertyValues [prop.Name] != null) { PropertyValues [prop.Name].PropertyValue = prop.PropertyValue; } else { PropertyValues.Add(prop); } } } OnSettingsLoaded(this, new SettingsLoadedEventArgs(provider)); }
/// <summary> /// Implementation of IClientSettingsProvider.Upgrade. /// Tries to locate a previous version of the user.config file. If found, it migrates matching settings. /// If not, it does nothing. /// </summary> public void Upgrade(SettingsContext context, SettingsPropertyCollection properties) { // Separate the local and roaming settings and upgrade them separately. SettingsPropertyCollection local = new SettingsPropertyCollection(); SettingsPropertyCollection roaming = new SettingsPropertyCollection(); foreach (SettingsProperty sp in properties) { bool isRoaming = IsRoamingSetting(sp); if (isRoaming) { roaming.Add(sp); } else { local.Add(sp); } } if (roaming.Count > 0) { Upgrade(context, roaming, true); } if (local.Count > 0) { Upgrade(context, local, false); } }
private void LoadPropertyValue(SettingsPropertyCollection collection, SettingElement element, bool allowOverwrite) { SettingsProperty prop = collection [element.Name]; if (prop == null) // see bug #343459 { prop = new SettingsProperty(element.Name); collection.Add(prop); } SettingsPropertyValue value = new SettingsPropertyValue(prop); value.IsDirty = false; value.SerializedValue = element.Value.ValueXml != null ? element.Value.ValueXml.InnerText : prop.DefaultValue; try { if (allowOverwrite) { values.Remove(element.Name); } values.Add(value); } catch (ArgumentException) { throw new ConfigurationErrorsException(); } }
public static SettingsPropertyCollection ToSettingsPropertyCollection(this IEnumerable<SettingsProperty> settingsProperties) { var c = new SettingsPropertyCollection(); foreach (var property in settingsProperties) { c.Add(property); } return c; }
private static SettingsPropertyCollection GetPropertiesForProvider(ApplicationSettingsBase settings, SettingsProvider provider) { SettingsPropertyCollection properties = new SettingsPropertyCollection(); foreach (SettingsProperty property in settings.Properties) { if (property.Provider == provider) properties.Add(property); } return properties; }
private SettingsPropertyCollection GetPropertiesForProvider(SettingsProvider provider) { SettingsPropertyCollection propertys = new SettingsPropertyCollection(); foreach (SettingsProperty property in this.Properties) { if (property.Provider == provider) { propertys.Add(property); } } return(propertys); }
private void LoadPropertyValue(SettingsPropertyCollection collection, SettingElement element, bool allowOverwrite) { SettingsProperty prop = collection [element.Name]; if (prop == null) // see bug #343459 { prop = new SettingsProperty(element.Name); collection.Add(prop); } SettingsPropertyValue value = new SettingsPropertyValue(prop); value.IsDirty = false; if (element.Value.ValueXml != null) { switch (value.Property.SerializeAs) { case SettingsSerializeAs.Xml: value.SerializedValue = element.Value.ValueXml.InnerXml; break; case SettingsSerializeAs.String: value.SerializedValue = element.Value.ValueXml.InnerText; break; case SettingsSerializeAs.Binary: value.SerializedValue = Convert.FromBase64String(element.Value.ValueXml.InnerText); break; } } else { value.SerializedValue = prop.DefaultValue; } try { if (allowOverwrite) { values.Remove(element.Name); } values.Add(value); } catch (ArgumentException ex) { throw new ConfigurationErrorsException(string.Format( CultureInfo.InvariantCulture, "Failed to load value for '{0}'.", element.Name), ex); } }
private SettingsPropertyCollection GetPropertiesForProvider(SettingsProvider provider) { SettingsPropertyCollection properties = new SettingsPropertyCollection(); foreach (SettingsProperty sp in Properties) { if (sp.Provider == provider) { properties.Add(sp); } } return(properties); }
public SettingsPropertyValue GetPreviousVersion(SettingsContext context, SettingsProperty property) { bool isRoaming = IsRoamingSetting(property); string previousConfigFileName = this.GetPreviousConfigFileName(isRoaming); if (!string.IsNullOrEmpty(previousConfigFileName)) { SettingsPropertyCollection properties = new SettingsPropertyCollection(); properties.Add(property); return(this.GetSettingValuesFromFile(previousConfigFileName, this.GetSectionName(context), 1, properties)[property.Name]); } return(new SettingsPropertyValue(property) { PropertyValue = null }); }
/// <summary> /// Ensures this class is initialized. Initialization involves reflecting over properties and building /// a list of SettingsProperty's. /// </summary> private void EnsureInitialized() { // Initialization method - // be careful not to access properties here to prevent stack overflow. if (!_initialized) { _initialized = true; Type type = GetType(); if (_context == null) { _context = new SettingsContext(); } _context["GroupName"] = type.FullName; _context["SettingsKey"] = SettingsKey; _context["SettingsClassType"] = type; PropertyInfo[] properties = SettingsFilter(type.GetProperties(BindingFlags.Instance | BindingFlags.Public)); _classAttributes = type.GetCustomAttributes(false); if (_settings == null) { _settings = new SettingsPropertyCollection(); } if (_providers == null) { _providers = new SettingsProviderCollection(); } for (int i = 0; i < properties.Length; i++) { SettingsProperty sp = CreateSetting(properties[i]); if (sp != null) { _settings.Add(sp); if (sp.Provider != null && _providers[sp.Provider.Name] == null) { _providers.Add(sp.Provider); } } } } }
private void Upgrade(SettingsContext context, SettingsPropertyCollection properties, bool isRoaming) { string previousConfigFileName = this.GetPreviousConfigFileName(isRoaming); if (!string.IsNullOrEmpty(previousConfigFileName)) { SettingsPropertyCollection propertys = new SettingsPropertyCollection(); foreach (SettingsProperty property in properties) { if (!(property.Attributes[typeof(NoSettingsVersionUpgradeAttribute)] is NoSettingsVersionUpgradeAttribute)) { propertys.Add(property); } } SettingsPropertyValueCollection collection = this.GetSettingValuesFromFile(previousConfigFileName, this.GetSectionName(context), true, propertys); this.SetPropertyValues(context, collection); } }
/// <summary> /// Implementation of IClientSettingsProvider.GetPreviousVersion. /// </summary> public SettingsPropertyValue GetPreviousVersion(SettingsContext context, SettingsProperty property) { bool isRoaming = IsRoamingSetting(property); string prevConfig = GetPreviousConfigFileName(isRoaming); if (!string.IsNullOrEmpty(prevConfig)) { SettingsPropertyCollection properties = new SettingsPropertyCollection(); properties.Add(property); SettingsPropertyValueCollection values = GetSettingValuesFromFile(prevConfig, GetSectionName(context), true, properties); return(values[property.Name]); } else { SettingsPropertyValue value = new SettingsPropertyValue(property); value.PropertyValue = null; return(value); } }
/// <summary> /// Private version of upgrade that uses isRoaming to determine which config file to use. /// </summary> private void Upgrade(SettingsContext context, SettingsPropertyCollection properties, bool isRoaming) { string prevConfig = GetPreviousConfigFileName(isRoaming); if (!string.IsNullOrEmpty(prevConfig)) { //Filter the settings properties to exclude those that have a NoSettingsVersionUpgradeAttribute on them. SettingsPropertyCollection upgradeProperties = new SettingsPropertyCollection(); foreach (SettingsProperty sp in properties) { if (!(sp.Attributes[typeof(NoSettingsVersionUpgradeAttribute)] is NoSettingsVersionUpgradeAttribute)) { upgradeProperties.Add(sp); } } SettingsPropertyValueCollection values = GetSettingValuesFromFile(prevConfig, GetSectionName(context), true, upgradeProperties); SetPropertyValues(context, values); } }
private void GetPropertiesFromProvider(SettingsProvider provider) { SettingsPropertyCollection collection = new SettingsPropertyCollection(); foreach (SettingsProperty property in this.Properties) { if (property.Provider == provider) { collection.Add(property); } } if (collection.Count > 0) { foreach (SettingsPropertyValue value2 in provider.GetPropertyValues(this.Context, collection)) { if (this._PropertyValues[value2.Name] == null) { this._PropertyValues.Add(value2); } } } }
private void GetPropertiesFromProvider(SettingsProvider provider) { SettingsPropertyCollection ppc = new SettingsPropertyCollection(); foreach (SettingsProperty pp in Properties) { if (pp.Provider == provider) { ppc.Add(pp); } } if (ppc.Count > 0) { SettingsPropertyValueCollection ppcv = provider.GetPropertyValues(Context, ppc); foreach (SettingsPropertyValue p in ppcv) { if (_PropertyValues[p.Name] == null) { _PropertyValues.Add(p); } } } }
public void AuthenticatedDateTime() { ProfileBase profile = ProfileBase.Create("foo", true); ResetAppId(profile.Providers["MySqlProfileProvider"] as MySQLProfileProvider); DateTime date = DateTime.Now; profile["BirthDate"] = date; profile.Save(); SettingsPropertyCollection getProps = new SettingsPropertyCollection(); SettingsProperty getProp1 = new SettingsProperty("BirthDate"); getProp1.PropertyType = typeof(DateTime); getProp1.SerializeAs = SettingsSerializeAs.Xml; getProps.Add(getProp1); MySQLProfileProvider provider = InitProfileProvider(); SettingsContext ctx = new SettingsContext(); ctx.Add("IsAuthenticated", true); ctx.Add("UserName", "foo"); SettingsPropertyValueCollection getValues = provider.GetPropertyValues(ctx, getProps); Assert.AreEqual(1, getValues.Count); SettingsPropertyValue getValue1 = getValues["BirthDate"]; Assert.AreEqual(date, getValue1.PropertyValue); }
public void AddPropertyNoProviderButInProviders () { SettingsPropertyCollection props = new SettingsPropertyCollection (); SettingsProviderCollection provs = new SettingsProviderCollection (); MyProvider p = new MyProvider (); MySettings s = new MySettings (); props.Add (new SettingsProperty ("Foo", typeof (string), null, false, 10, SettingsSerializeAs.String, null, true, true)); provs.Add (p); s.Initialize (new SettingsContext (), props, provs); Assert.AreEqual (100, s.Foo); }
void CacheValuesByProvider (SettingsProvider provider) { SettingsPropertyCollection col = new SettingsPropertyCollection (); foreach (SettingsProperty p in Properties) { if (p.Provider == provider) col.Add (p); } if (col.Count > 0) { SettingsPropertyValueCollection vals = provider.GetPropertyValues (Context, col); PropertyValues.Add (vals); } OnSettingsLoaded (this, new SettingsLoadedEventArgs (provider)); }
void CreateSettingsProperty (PropertyInfo prop, SettingsPropertyCollection properties, ref LocalFileSettingsProvider local_provider) { SettingsAttributeDictionary dict = new SettingsAttributeDictionary (); SettingsProvider provider = null; object defaultValue = null; SettingsSerializeAs serializeAs = SettingsSerializeAs.String; bool explicitSerializeAs = false; foreach (Attribute a in prop.GetCustomAttributes (false)) { /* the attributes we handle natively here */ if (a is SettingsProviderAttribute) { Type provider_type = Type.GetType (((SettingsProviderAttribute)a).ProviderTypeName); provider = (SettingsProvider) Activator.CreateInstance (provider_type); provider.Initialize (null, null); } else if (a is DefaultSettingValueAttribute) { defaultValue = ((DefaultSettingValueAttribute)a).Value; } else if (a is SettingsSerializeAsAttribute) { serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs; explicitSerializeAs = true; } else if (a is ApplicationScopedSettingAttribute || a is UserScopedSettingAttribute) { dict.Add (a.GetType(), a); } else { dict.Add (a.GetType(), a); } } if (!explicitSerializeAs) { // DefaultValue is a string and if we can't convert from string to the // property type then the only other option left is for the string to // be XML. // TypeConverter converter = TypeDescriptor.GetConverter (prop.PropertyType); if (converter != null && (!converter.CanConvertFrom (typeof (string)) || !converter.CanConvertTo (typeof (string)))) serializeAs = SettingsSerializeAs.Xml; } SettingsProperty setting = new SettingsProperty (prop.Name, prop.PropertyType, provider, false /* XXX */, defaultValue /* XXX always a string? */, serializeAs, dict, false, false); if (providerService != null) setting.Provider = providerService.GetSettingsProvider (setting); if (provider == null) { if (local_provider == null) { local_provider = new LocalFileSettingsProvider (); local_provider.Initialize (null, null); } setting.Provider = local_provider; // .NET ends up to set this to providers. provider = local_provider; } if (provider != null) { /* make sure we're using the same instance of a given provider across multiple properties */ SettingsProvider p = Providers[provider.Name]; if (p != null) setting.Provider = p; } properties.Add (setting); if (setting.Provider != null && Providers [setting.Provider.Name] == null) Providers.Add (setting.Provider); }
public void AuthenticatedStringProperty() { ProfileBase profile = ProfileBase.Create("foo", true); ResetAppId(profile.Providers["MySqlProfileProvider"] as MySQLProfileProvider); profile["Name"] = "Fred Flintstone"; profile.Save(); SettingsPropertyCollection getProps = new SettingsPropertyCollection(); SettingsProperty getProp1 = new SettingsProperty("Name"); getProp1.PropertyType = typeof(String); getProps.Add(getProp1); MySQLProfileProvider provider = InitProfileProvider(); SettingsContext ctx = new SettingsContext(); ctx.Add("IsAuthenticated", true); ctx.Add("UserName", "foo"); SettingsPropertyValueCollection getValues = provider.GetPropertyValues(ctx, getProps); Assert.AreEqual(1, getValues.Count); SettingsPropertyValue getValue1 = getValues["Name"]; Assert.AreEqual("Fred Flintstone", getValue1.PropertyValue); }
public void GetAllProfiles() { ProfileBase profile = ProfileBase.Create("foo", true); ResetAppId(profile.Providers["MySqlProfileProvider"] as MySQLProfileProvider); profile["Name"] = "Fred Flintstone"; profile.Save(); SettingsPropertyCollection getProps = new SettingsPropertyCollection(); SettingsProperty getProp1 = new SettingsProperty("Name"); getProp1.PropertyType = typeof(String); getProps.Add(getProp1); MySQLProfileProvider provider = InitProfileProvider(); SettingsContext ctx = new SettingsContext(); ctx.Add("IsAuthenticated", true); ctx.Add("UserName", "foo"); int total; ProfileInfoCollection profiles = provider.GetAllProfiles( ProfileAuthenticationOption.All, 0, 10, out total); Assert.AreEqual(1, total); }
void CreateSettingsProperty(PropertyInfo prop, SettingsPropertyCollection properties, ref SettingsProvider local_provider) { SettingsAttributeDictionary dict = new SettingsAttributeDictionary(); SettingsProvider provider = null; object defaultValue = null; SettingsSerializeAs serializeAs = SettingsSerializeAs.String; bool explicitSerializeAs = false; foreach (Attribute a in prop.GetCustomAttributes(false)) { /* the attributes we handle natively here */ if (a is SettingsProviderAttribute) { var providerTypeName = ((SettingsProviderAttribute)a).ProviderTypeName; Type provider_type = Type.GetType(providerTypeName); if (provider_type == null) // Type failed to find the type by name { var typeNameParts = providerTypeName.Split('.'); if (typeNameParts.Length > 1) //Load the assembly that providerTypeName claims { var assy = Assembly.Load(typeNameParts[0]); if (assy != null) { provider_type = assy.GetType(providerTypeName); //try to get the type from that Assembly } } } provider = (SettingsProvider)Activator.CreateInstance(provider_type); provider.Initialize(null, null); } else if (a is DefaultSettingValueAttribute) { defaultValue = ((DefaultSettingValueAttribute)a).Value; } else if (a is SettingsSerializeAsAttribute) { serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs; explicitSerializeAs = true; } else if (a is ApplicationScopedSettingAttribute || a is UserScopedSettingAttribute) { dict.Add(a.GetType(), a); } else { dict.Add(a.GetType(), a); } } if (!explicitSerializeAs) { // DefaultValue is a string and if we can't convert from string to the // property type then the only other option left is for the string to // be XML. // TypeConverter converter = TypeDescriptor.GetConverter(prop.PropertyType); if (converter != null && (!converter.CanConvertFrom(typeof(string)) || !converter.CanConvertTo(typeof(string)))) { serializeAs = SettingsSerializeAs.Xml; } } SettingsProperty setting = new SettingsProperty(prop.Name, prop.PropertyType, provider, false /* XXX */, defaultValue /* XXX always a string? */, serializeAs, dict, false, false); if (providerService != null) { setting.Provider = providerService.GetSettingsProvider(setting); } if (provider == null) { if (local_provider == null) { local_provider = new LocalFileSettingsProvider() as SettingsProvider; local_provider.Initialize(null, null); } setting.Provider = local_provider; // .NET ends up to set this to providers. provider = local_provider; } if (provider != null) { /* make sure we're using the same instance of a * given provider across multiple properties */ SettingsProvider p = Providers[provider.Name]; if (p != null) { setting.Provider = p; } } properties.Add(setting); if (setting.Provider != null && Providers [setting.Provider.Name] == null) { Providers.Add(setting.Provider); } }
public void StringCollectionAsProperty() { ProfileBase profile = ProfileBase.Create("foo", true); ResetAppId(profile.Providers["MySqlProfileProvider"] as MySQLProfileProvider); StringCollection colors = new StringCollection(); colors.Add("red"); colors.Add("green"); colors.Add("blue"); profile["FavoriteColors"] = colors; profile.Save(); DataTable dt = FillTable("SELECT * FROM my_aspnet_applications"); Assert.AreEqual(1, dt.Rows.Count); dt = FillTable("SELECT * FROM my_aspnet_users"); Assert.AreEqual(1, dt.Rows.Count); dt = FillTable("SELECT * FROM my_aspnet_profiles"); Assert.AreEqual(1, dt.Rows.Count); // now retrieve them SettingsPropertyCollection getProps = new SettingsPropertyCollection(); SettingsProperty getProp1 = new SettingsProperty("FavoriteColors"); getProp1.PropertyType = typeof(StringCollection); getProp1.SerializeAs = SettingsSerializeAs.Xml; getProps.Add(getProp1); MySQLProfileProvider provider = InitProfileProvider(); SettingsContext ctx = new SettingsContext(); ctx.Add("IsAuthenticated", true); ctx.Add("UserName", "foo"); SettingsPropertyValueCollection getValues = provider.GetPropertyValues(ctx, getProps); Assert.AreEqual(1, getValues.Count); SettingsPropertyValue getValue1 = getValues["FavoriteColors"]; StringCollection outValue = (StringCollection)getValue1.PropertyValue; Assert.AreEqual(3, outValue.Count); Assert.AreEqual("red", outValue[0]); Assert.AreEqual("green", outValue[1]); Assert.AreEqual("blue", outValue[2]); }
private void Upgrade(SettingsContext context, SettingsPropertyCollection properties, bool isRoaming) { string prevConfig = GetPreviousConfigFileName(isRoaming); if (!String.IsNullOrEmpty(prevConfig)) { //Filter the settings properties to exclude those that have a NoSettingsVersionUpgradeAttribute on them. SettingsPropertyCollection upgradeProperties = new SettingsPropertyCollection(); foreach (SettingsProperty sp in properties) { if (!(sp.Attributes[typeof(NoSettingsVersionUpgradeAttribute)] is NoSettingsVersionUpgradeAttribute)) { upgradeProperties.Add(sp); } } SettingsPropertyValueCollection values = GetSettingValuesFromFile(prevConfig, GetSectionName(context), true, upgradeProperties); SetPropertyValues(context, values); } }
/// <devdoc> /// Implementation of IClientSettingsProvider.Upgrade. /// Tries to locate a previous version of the user.config file. If found, it migrates matching settings. /// If not, it does nothing. /// </devdoc> public void Upgrade(SettingsContext context, SettingsPropertyCollection properties) { // Separate the local and roaming settings and upgrade them separately. SettingsPropertyCollection local = new SettingsPropertyCollection(); SettingsPropertyCollection roaming = new SettingsPropertyCollection(); foreach (SettingsProperty sp in properties) { bool isRoaming = IsRoamingSetting(sp); if (isRoaming) { roaming.Add(sp); } else { local.Add(sp); } } if (roaming.Count > 0) { Upgrade(context, roaming, true); } if (local.Count > 0) { Upgrade(context, local, false); } }
private void LoadPropertyValue (SettingsPropertyCollection collection, SettingElement element, bool allowOverwrite) { SettingsProperty prop = collection [element.Name]; if (prop == null) { // see bug #343459 prop = new SettingsProperty (element.Name); collection.Add (prop); } SettingsPropertyValue value = new SettingsPropertyValue (prop); value.IsDirty = false; if (element.Value.ValueXml != null) { switch (value.Property.SerializeAs) { case SettingsSerializeAs.Xml: value.SerializedValue = element.Value.ValueXml.InnerXml; break; case SettingsSerializeAs.String: value.SerializedValue = element.Value.ValueXml.InnerText; break; case SettingsSerializeAs.Binary: value.SerializedValue = Convert.FromBase64String (element.Value.ValueXml.InnerText); break; } } else value.SerializedValue = prop.DefaultValue; try { if (allowOverwrite) values.Remove (element.Name); values.Add (value); } catch (ArgumentException ex) { throw new ConfigurationErrorsException (string.Format ( CultureInfo.InvariantCulture, "Failed to load value for '{0}'.", element.Name), ex); } }
public SettingsPropertyValue GetPreviousVersion(SettingsContext context, SettingsProperty property) { bool isRoaming = IsRoamingSetting(property); string prevConfig = GetPreviousConfigFileName(isRoaming); if (!String.IsNullOrEmpty(prevConfig)) { SettingsPropertyCollection properties = new SettingsPropertyCollection(); properties.Add(property); SettingsPropertyValueCollection values = GetSettingValuesFromFile(prevConfig, GetSectionName(context), true, properties); return values[property.Name]; } else { SettingsPropertyValue value = new SettingsPropertyValue(property); value.PropertyValue = null; return value; } }
////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// static private void InitializeStatic() { if (!ProfileManager.Enabled || s_Initialized) { if (s_InitializeException != null) throw s_InitializeException; return; } lock (s_InitializeLock) { if (s_Initialized) { if (s_InitializeException != null) throw s_InitializeException; return; } try { ProfileSection config = MTConfigUtil.GetProfileAppConfig(); bool fAnonEnabled = (HostingEnvironment.IsHosted ? AnonymousIdentificationModule.Enabled : true); Type baseType = ProfileBase.InheritsFromType; bool hasLowTrust = HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Low); s_Properties = new SettingsPropertyCollection(); // Step 0: Add all dynamic profile properties set programatically during PreAppStart ProfileBase.AddPropertySettingsFromConfig(baseType, fAnonEnabled, hasLowTrust, ProfileManager.DynamicProfileProperties, null); ////////////////////////////////////////////////////////////////////// // Step 1: Add Properties from the base class (if not ProfileBase) if (baseType != typeof(ProfileBase)) { ////////////////////////////////////////////////////////////////////// // Step 2: Construct a hashtable containing a list of all the property-names in the ProfileBase type PropertyInfo[] baseProps = typeof(ProfileBase).GetProperties(); NameValueCollection baseProperties = new NameValueCollection(baseProps.Length); foreach (PropertyInfo baseProp in baseProps) baseProperties.Add(baseProp.Name, String.Empty); ////////////////////////////////////////////////////////////////////// // Step 3: For each property in the derived class, add it to the s_Properties class. PropertyInfo[] props = baseType.GetProperties(); foreach (PropertyInfo prop in props) { if (baseProperties[prop.Name] == null) { //not in the base class ProfileProvider prov = hasLowTrust ? ProfileManager.Provider : null; bool readOnly = false; SettingsSerializeAs serializeAs = SettingsSerializeAs.ProviderSpecific; string defaultValue = String.Empty; bool allowAnonymous = false; string customData = null; ////////////////////////////////////////////////////////////////////// // Step 4: For the property, get the attributes Attribute[] attribs = Attribute.GetCustomAttributes(prop, true); foreach (Attribute attrib in attribs) { if (attrib is SettingsSerializeAsAttribute) { serializeAs = ((SettingsSerializeAsAttribute)attrib).SerializeAs; } else if (attrib is SettingsAllowAnonymousAttribute) { allowAnonymous = ((SettingsAllowAnonymousAttribute)attrib).Allow; if (!fAnonEnabled && allowAnonymous) throw new ConfigurationErrorsException(SR.GetString(SR.Annoymous_id_module_not_enabled, prop.Name), config.ElementInformation.Properties["inherits"].Source, config.ElementInformation.Properties["inherits"].LineNumber); } else if (attrib is System.ComponentModel.ReadOnlyAttribute) { readOnly = ((System.ComponentModel.ReadOnlyAttribute)attrib).IsReadOnly; } else if (attrib is DefaultSettingValueAttribute) { defaultValue = ((DefaultSettingValueAttribute)attrib).Value; } else if (attrib is CustomProviderDataAttribute) { customData = ((CustomProviderDataAttribute)attrib).CustomProviderData; } else if (hasLowTrust && attrib is ProfileProviderAttribute) { prov = ProfileManager.Providers[((ProfileProviderAttribute)attrib).ProviderName]; if (prov == null) throw new ConfigurationErrorsException(SR.GetString(SR.Profile_provider_not_found, ((ProfileProviderAttribute)attrib).ProviderName), config.ElementInformation.Properties["inherits"].Source, config.ElementInformation.Properties["inherits"].LineNumber); } } ////////////////////////////////////////////////////////////////////// // Step 5: Add the property to the s_Properties SettingsAttributeDictionary settings = new SettingsAttributeDictionary(); settings.Add("AllowAnonymous", allowAnonymous); if (!string.IsNullOrEmpty(customData)) settings.Add("CustomProviderData", customData); SettingsProperty sp = new SettingsProperty(prop.Name, prop.PropertyType, prov, readOnly, defaultValue, serializeAs, settings, false, true); s_Properties.Add(sp); } } } ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// // Step 6: Add all properties from config if (config.PropertySettings != null) { AddPropertySettingsFromConfig(baseType, fAnonEnabled, hasLowTrust, config.PropertySettings, null); foreach (ProfileGroupSettings pgs in config.PropertySettings.GroupSettings) { AddPropertySettingsFromConfig(baseType, fAnonEnabled, hasLowTrust, pgs.PropertySettings, pgs.Name); } } } catch (Exception e) { if (s_InitializeException == null) s_InitializeException = e; } // If there are no properties, create an empty collection. if (s_Properties == null) s_Properties = new SettingsPropertyCollection(); // Make the properties collection read-only s_Properties.SetReadOnly(); s_Initialized = true; } // Throw an exception if there was an exception during initialization if (s_InitializeException != null) throw s_InitializeException; }
void CreateSettingsProperty(PropertyInfo prop, SettingsPropertyCollection properties, ref LocalFileSettingsProvider local_provider) { SettingsAttributeDictionary dict = new SettingsAttributeDictionary(); SettingsProvider provider = null; object defaultValue = null; SettingsSerializeAs serializeAs = SettingsSerializeAs.String; foreach (Attribute a in prop.GetCustomAttributes(false)) { /* the attributes we handle natively here */ if (a is SettingsProviderAttribute) { Type provider_type = Type.GetType(((SettingsProviderAttribute)a).ProviderTypeName); provider = (SettingsProvider)Activator.CreateInstance(provider_type); provider.Initialize(null, null); } else if (a is DefaultSettingValueAttribute) { defaultValue = ((DefaultSettingValueAttribute)a).Value; /* XXX this is a string.. do we convert? */ // note: for StringCollection, TypeDescriptor.GetConverter(prop.PropertyType) returns // CollectionConverter, however this class cannot handle the XML serialized strings if (prop.PropertyType == typeof(StringCollection)) { XmlSerializer xs = new XmlSerializer(typeof(string[])); string[] values = (string[])xs.Deserialize(new StringReader((string)defaultValue)); StringCollection sc = new StringCollection(); sc.AddRange(values); defaultValue = sc; } else if (prop.PropertyType != typeof(string)) { defaultValue = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromString((string)defaultValue); } } else if (a is SettingsSerializeAsAttribute) { serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs; } else if (a is ApplicationScopedSettingAttribute || a is UserScopedSettingAttribute) { dict.Add(a.GetType(), a); } else { dict.Add(a.GetType(), a); } } SettingsProperty setting = new SettingsProperty(prop.Name, prop.PropertyType, provider, false /* XXX */, defaultValue /* XXX always a string? */, serializeAs, dict, false, false); if (providerService != null) { setting.Provider = providerService.GetSettingsProvider(setting); } if (provider == null) { if (local_provider == null) { local_provider = new LocalFileSettingsProvider(); local_provider.Initialize(null, null); } setting.Provider = local_provider; // .NET ends up to set this to providers. provider = local_provider; } if (provider != null) { /* make sure we're using the same instance of a * given provider across multiple properties */ SettingsProvider p = Providers[provider.Name]; if (p != null) { setting.Provider = p; } } properties.Add(setting); if (setting.Provider != null && Providers [setting.Provider.Name] == null) { Providers.Add(setting.Provider); } }
public void Upgrade(SettingsContext context, SettingsPropertyCollection properties) { string previousUserConfigPath = PreviousUserConfigPath; if (!string.IsNullOrEmpty(previousUserConfigPath)) { SettingsPropertyCollection propertys = new SettingsPropertyCollection(); foreach (SettingsProperty property in properties) { if (!(property.Attributes[typeof(NoSettingsVersionUpgradeAttribute)] is NoSettingsVersionUpgradeAttribute)) { propertys.Add(property); } } SettingsPropertyValueCollection collection = this.GetPropertyValuesFromFile(previousUserConfigPath, context, propertys); this.SetPropertyValues(context, collection); } }
public SettingsPropertyValue GetPreviousVersion(SettingsContext context, SettingsProperty property) { string previousUserConfigPath = PreviousUserConfigPath; if (!string.IsNullOrEmpty(previousUserConfigPath)) { SettingsPropertyCollection properties = new SettingsPropertyCollection(); properties.Add(property); return this.GetPropertyValuesFromFile(previousUserConfigPath, context, properties)[property.Name]; } return new SettingsPropertyValue(property) { PropertyValue = null }; }
private void GetPropertiesFromProvider(SettingsProvider provider) { SettingsPropertyCollection ppc = new SettingsPropertyCollection(); foreach (SettingsProperty pp in Properties) { if (pp.Provider == provider) { ppc.Add(pp); } } if (ppc.Count > 0) { SettingsPropertyValueCollection ppcv = provider.GetPropertyValues(Context, ppc); foreach (SettingsPropertyValue p in ppcv) { if (_PropertyValues[p.Name] == null) _PropertyValues.Add(p); } } }
public void PropertyValuesInstance () { SettingsPropertyCollection props = new SettingsPropertyCollection (); SettingsProviderCollection provs = new SettingsProviderCollection (); MyProvider p = new MyProvider (); MySettings s = new MySettings (); props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true)); provs.Add (p); s.Initialize (new SettingsContext (), props, provs); Assert.AreEqual (s.PropertyValues, s.PropertyValues); }
public void ReadOnly_Clear () { SettingsPropertyCollection col = new SettingsPropertyCollection (); SettingsProperty test_prop = new SettingsProperty ("test_prop"); col.Add (test_prop); col.SetReadOnly (); col.Clear (); }
public SettingsPropertyValue GetPreviousVersion(SettingsContext context, SettingsProperty property) { bool isRoaming = IsRoamingSetting(property); string previousConfigFileName = this.GetPreviousConfigFileName(isRoaming); if (!string.IsNullOrEmpty(previousConfigFileName)) { SettingsPropertyCollection properties = new SettingsPropertyCollection(); properties.Add(property); return this.GetSettingValuesFromFile(previousConfigFileName, this.GetSectionName(context), 1, properties)[property.Name]; } return new SettingsPropertyValue(property) { PropertyValue = null }; }
public void Remove_NonExistant () { SettingsPropertyCollection col = new SettingsPropertyCollection (); SettingsProperty test_prop = new SettingsProperty ("test_prop"); col.Add (test_prop); Assert.AreEqual (1, col.Count, "A1"); col.Remove ("test_prop2"); Assert.AreEqual (1, col.Count, "A2"); }
public void AddPropertyTypeMismatch () { SettingsPropertyCollection props = new SettingsPropertyCollection (); SettingsProviderCollection provs = new SettingsProviderCollection (); MyProvider p = new MyProvider (); MySettings s = new MySettings (); props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true)); provs.Add (p); s.Initialize (new SettingsContext (), props, provs); int i = s.Foo; // it still works as int, regardless of the settings property type... }
public void ExceptionalGetPropertyValues () { SettingsPropertyCollection props = new SettingsPropertyCollection (); SettingsProviderCollection provs = new SettingsProviderCollection (); MyProvider3 p = new MyProvider3 (); MySettings s = new MySettings (); props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true)); provs.Add (p); s.Initialize (new SettingsContext (), props, provs); Assert.AreEqual (0, s.Context.Count, "#0"); try { Assert.AreEqual (100, s.Foo, "#1"); Assert.Fail ("#2"); #if !TARGET_JVM } catch (Win32Exception) { #else } catch (CustomerException) { #endif } }
private static void AddToColl(ProfilePropertyMetadata p, SettingsPropertyCollection retColl, bool isAuthenticated) { string propName = p.PropertyName; Type propType = Type.GetType(p.TypeName, false, true); bool allowAnon = p.AllowAnonymousAccess; bool readOnly = p.IsReadOnly; if (!allowAnon && !isAuthenticated) return; SettingsSerializeAs serializeAs = (SettingsSerializeAs)p.SerializeAs; SettingsAttributeDictionary dict = new SettingsAttributeDictionary(); dict.Add("AllowAnonymous", allowAnon); retColl.Add(new SettingsProperty(propName, propType, null, readOnly,p.DefaultValue, serializeAs, dict, true, true)); }
public void PropertyValuesInitialized () { SettingsPropertyCollection props = new SettingsPropertyCollection (); SettingsProviderCollection provs = new SettingsProviderCollection (); MyProvider p = new MyProvider (); MySettings s = new MySettings (); int i; try { i = s.Foo; Assert.Fail ("#1-2"); } catch (SettingsPropertyNotFoundException) { } s.Initialize (new SettingsContext (), props, provs); Assert.AreEqual (0, s.PropertyValues.Count, "#2-1"); Assert.AreEqual (0, s.Context.Count, "#2-2"); props.Add (new SettingsProperty ("Foo", typeof (int), p, false, 10, SettingsSerializeAs.String, null, true, true)); // initialize w/o the provider s.Initialize (new SettingsContext (), props, provs); Assert.AreEqual (0, s.PropertyValues.Count, "#3-0"); Assert.AreEqual (100, s.Foo, "#3-1"); // ... !!! Assert.AreEqual (1, s.PropertyValues.Count, "#3-2"); SettingsPropertyValue v = s.PropertyValues ["Foo"]; Assert.AreEqual (100, v.PropertyValue, "#3-3"); Assert.AreEqual (0, s.Context.Count, "#3-4"); // initialize w/ the provider provs.Add (p); provs.Add (new MyProvider2 ("Bar", 25)); props.Add (new SettingsProperty ("Bar", typeof (int), provs ["MyProvider2"], false, 10, SettingsSerializeAs.String, null, true, true)); s.Initialize (new SettingsContext (), props, provs); Assert.AreEqual (1, s.PropertyValues.Count, "#4-1"); Assert.AreEqual (100, s.Foo, "#4-2"); Assert.AreEqual (25, s.Bar, "#4-3"); // ... !!! Assert.AreEqual (2, s.PropertyValues.Count, "#4-3-2"); Assert.AreEqual (0, s.Context.Count, "#4-4"); // wrong provider props.Remove ("Bar"); props.Add (new SettingsProperty ("Bar", typeof (int), provs ["MyProvider"], false, 10, SettingsSerializeAs.String, null, true, true)); s = new MySettings (); s.Initialize (new SettingsContext (), props, provs); Assert.AreEqual (0, s.PropertyValues.Count, "#5-1"); Assert.AreEqual (100, s.Foo, "#5-2"); Assert.AreEqual (10, s.Bar, "#5-3"); }