Initialize() public méthode

public Initialize ( string name, NameValueCollection values ) : void
name string
values NameValueCollection
Résultat void
		} // ApplicationSettings

		// ----------------------------------------------------------------------
		public ApplicationSettings( string settingsKey, object obj ) :
			base( settingsKey )
		{
			settings = new SettingCollection( this );

			// provider
			defaultProvider = new LocalFileSettingsProvider();
			defaultProvider.Initialize( "LocalFileSettingsProvider", new NameValueCollection() );
			base.Providers.Add( defaultProvider );

			// upgrade
			upgradeSettings = new ValueSetting( UpgradeSettingsKey, typeof( bool ), true );
			UseAutoUpgrade = true;

			if ( obj != null )
			{
				Settings.AddAll( obj );
			}
		} // ApplicationSettings
		public void Initialized ()
		{
			LocalFileSettingsProvider prov = new LocalFileSettingsProvider ();

			prov.Initialize (null, null);

			// defaults, uninitialized
			Assert.AreEqual ("LocalFileSettingsProvider", prov.Name, "A1");
			Assert.AreEqual ("", prov.ApplicationName, "A2");

			prov = new LocalFileSettingsProvider ();
			NameValueCollection nv = new NameValueCollection ();
			nv.Add ("applicationName", "appName");

			prov.Initialize ("hi", nv);
			// As these lines below shows, Initialize() behavior is unpredictable. Here I just comment out them and fix run-test-ondotnet tests.
			//Assert.AreEqual ("hi", prov.Name, "A3");
			//Assert.AreEqual ("hi", prov.Description, "A3.5");
			//Assert.AreEqual ("", prov.ApplicationName, "A4");
		}
		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);
		}
        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 GetUserScopedPropertyValues ()
		{
			SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
			UserScopedSettingAttribute attr = new UserScopedSettingAttribute ();
			dict.Add (attr.GetType(), attr);

			LocalFileSettingsProvider prov = new LocalFileSettingsProvider ();
			SettingsContext ctx = new SettingsContext ();
			SettingsProperty p = new SettingsProperty ("property",
								   typeof (int),
								   prov,
								   false,
								   10,
								   SettingsSerializeAs.Binary,
								   dict,
								   false,
								   false);
			SettingsPropertyCollection col = new SettingsPropertyCollection ();
			SettingsPropertyValueCollection vals;

			col.Add (p);

			prov.Initialize (null, null);

			vals = prov.GetPropertyValues (ctx, col);
			Assert.IsNotNull (vals, "A1");
			Assert.AreEqual (1, vals.Count, "A2");
		}
Exemple #6
0
        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);
            }
        }