private SettingsProperty CreateSetting(PropertyInfo propInfo) { object[] customAttributes = propInfo.GetCustomAttributes(false); SettingsProperty property = new SettingsProperty(this.Initializer); bool flag = this._explicitSerializeOnClass; property.Name = propInfo.Name; property.PropertyType = propInfo.PropertyType; for (int i = 0; i < customAttributes.Length; i++) { Attribute attribute = customAttributes[i] as Attribute; if (attribute != null) { if (attribute is DefaultSettingValueAttribute) { property.DefaultValue = ((DefaultSettingValueAttribute)attribute).Value; } else if (attribute is ReadOnlyAttribute) { property.IsReadOnly = true; } else if (attribute is SettingsProviderAttribute) { string providerTypeName = ((SettingsProviderAttribute)attribute).ProviderTypeName; Type type = Type.GetType(providerTypeName); if (type == null) { throw new ConfigurationErrorsException(System.SR.GetString("ProviderTypeLoadFailed", new object[] { providerTypeName })); } SettingsProvider provider = SecurityUtils.SecureCreateInstance(type) as SettingsProvider; if (provider == null) { throw new ConfigurationErrorsException(System.SR.GetString("ProviderInstantiationFailed", new object[] { providerTypeName })); } provider.Initialize(null, null); provider.ApplicationName = ConfigurationManagerInternalFactory.Instance.ExeProductName; SettingsProvider provider2 = this._providers[provider.Name]; if (provider2 != null) { provider = provider2; } property.Provider = provider; } else if (attribute is SettingsSerializeAsAttribute) { property.SerializeAs = ((SettingsSerializeAsAttribute)attribute).SerializeAs; flag = true; } else { property.Attributes.Add(attribute.GetType(), attribute); } } } if (!flag) { property.SerializeAs = this.GetSerializeAs(propInfo.PropertyType); } return(property); }
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); } }
///<summary> ///Initializes the provider. ///</summary> /// ///<param name="config">A collection of the name/value pairs representing the provider-specific attributes specified in the configuration for this provider.</param> ///<param name="name">The friendly name of the provider.</param> public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) { lock (_syncLock) { // obtain a source provider try { ISettingsStore store = SettingsStore.Create(); _sourceProvider = new SettingsStoreSettingsProvider(store); } catch (NotSupportedException) { //note: it's the log so keep it in English Platform.Log(LogLevel.Warn, "Configuration store not found - defaulting to LocalFileSettingsProvider" ); // default to LocalFileSettingsProvider as a last resort _sourceProvider = new ExtendedLocalFileSettingsProvider(new LocalFileSettingsProvider()); } // init source provider // according to sample implementations, use the application name here _sourceProvider.Initialize(this.ApplicationName, config); base.Initialize(this.ApplicationName, config); } }
/// <summary> /// Creates a SettingsProperty object using the metadata on the given property /// and returns it. /// /// Implementation note: Initialization method - be careful not to access properties here /// to prevent stack overflow. /// </summary> private SettingsProperty CreateSetting(PropertyInfo propInfo) { object[] attributes = propInfo.GetCustomAttributes(false); SettingsProperty sp = new SettingsProperty(Initializer); bool explicitSerialize = _explicitSerializeOnClass; sp.Name = propInfo.Name; sp.PropertyType = propInfo.PropertyType; for (int i = 0; i < attributes.Length; i++) { Attribute attr = attributes[i] as Attribute; if (attr != null) { if (attr is DefaultSettingValueAttribute) { sp.DefaultValue = ((DefaultSettingValueAttribute)attr).Value; } else if (attr is ReadOnlyAttribute) { sp.IsReadOnly = true; } else if (attr is SettingsProviderAttribute) { string providerTypeName = ((SettingsProviderAttribute)attr).ProviderTypeName; Type providerType = Type.GetType(providerTypeName); if (providerType != null) { SettingsProvider spdr = TypeUtil.CreateInstance(providerType) as SettingsProvider; if (spdr != null) { spdr.Initialize(null, null); spdr.ApplicationName = ConfigurationManagerInternalFactory.Instance.ExeProductName; // See if we already have a provider of the same name in our collection. If so, // re-use the existing instance, since we cannot have multiple providers of the same name. SettingsProvider existing = _providers[spdr.Name]; if (existing != null) { spdr = existing; } sp.Provider = spdr; } else { throw new ConfigurationErrorsException(string.Format(SR.ProviderInstantiationFailed, providerTypeName)); } } else { throw new ConfigurationErrorsException(string.Format(SR.ProviderTypeLoadFailed, providerTypeName)); } } else if (attr is SettingsSerializeAsAttribute) { sp.SerializeAs = ((SettingsSerializeAsAttribute)attr).SerializeAs; explicitSerialize = true; } else { // This isn't an attribute we care about, so simply pass it on // to the SettingsProvider. // NOTE: The key is the type. So if an attribute was found at class // level and also property level, the latter overrides the former // for a given setting. This is exactly the behavior we want. sp.Attributes.Add(attr.GetType(), attr); } } } if (!explicitSerialize) { sp.SerializeAs = GetSerializeAs(propInfo.PropertyType); } return(sp); }
/// <summary> /// Creates a SettingsProperty object using the metadata on the given property /// and returns it. /// </summary> private SettingsProperty CreateSetting(PropertyInfo propertyInfo) { // Initialization method - // be careful not to access properties here to prevent stack overflow. object[] attributes = propertyInfo.GetCustomAttributes(false); SettingsProperty settingsProperty = new SettingsProperty(Initializer); bool explicitSerialize = _explicitSerializeOnClass; settingsProperty.Name = propertyInfo.Name; settingsProperty.PropertyType = propertyInfo.PropertyType; for (int i = 0; i < attributes.Length; i++) { Attribute attribute = attributes[i] as Attribute; if (attribute == null) { continue; } if (attribute is DefaultSettingValueAttribute) { settingsProperty.DefaultValue = ((DefaultSettingValueAttribute)attribute).Value; } else if (attribute is ReadOnlyAttribute) { settingsProperty.IsReadOnly = true; } else if (attribute is SettingsProviderAttribute) { string providerTypeName = ((SettingsProviderAttribute)attribute).ProviderTypeName; Type providerType = Type.GetType(providerTypeName); if (providerType == null) { throw new ConfigurationErrorsException(SR.Format(SR.ProviderTypeLoadFailed, providerTypeName)); } SettingsProvider settingsProvider = TypeUtil.CreateInstance(providerType) as SettingsProvider; if (settingsProvider == null) { throw new ConfigurationErrorsException(SR.Format(SR.ProviderInstantiationFailed, providerTypeName)); } settingsProvider.Initialize(null, null); settingsProvider.ApplicationName = ConfigurationManagerInternalFactory.Instance.ExeProductName; // See if we already have a provider of the same name in our collection. If so, // re-use the existing instance, since we cannot have multiple providers of the same name. SettingsProvider existing = _providers[settingsProvider.Name]; if (existing != null) { settingsProvider = existing; } settingsProperty.Provider = settingsProvider; } else if (attribute is SettingsSerializeAsAttribute) { settingsProperty.SerializeAs = ((SettingsSerializeAsAttribute)attribute).SerializeAs; explicitSerialize = true; } else { // This isn't an attribute we care about, so simply pass it on // to the SettingsProvider. // // NOTE: The key is the type. So if an attribute was found at class // level and also property level, the latter overrides the former // for a given setting. This is exactly the behavior we want. settingsProperty.Attributes.Add(attribute.GetType(), attribute); } } if (!explicitSerialize) { // Serialization method was not explicitly attributed. TypeConverter tc = TypeDescriptor.GetConverter(propertyInfo.PropertyType); if (tc.CanConvertTo(typeof(string)) && tc.CanConvertFrom(typeof(string))) { // We can use string settingsProperty.SerializeAs = SettingsSerializeAs.String; } else { // Fallback is Xml settingsProperty.SerializeAs = SettingsSerializeAs.Xml; } } return(settingsProperty); }
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); } }