Example #1
0
        // PortableSettingsProvider code obtained from http://stackoverflow.com/a/2579399 (Accessed 02-01-2014 @ 17:04).
        private static void MakeSettingsPortable(System.Configuration.ApplicationSettingsBase settings)
        {
            var portableSettingsProvider = new PortableSettingsProvider(settings.GetType().Name + ".settings");

            settings.Providers.Add(portableSettingsProvider);
            foreach (System.Configuration.SettingsProperty prop in settings.Properties)
            {
                prop.Provider = portableSettingsProvider;
            }
            settings.Reload();
        }
Example #2
0
        public void ApplyAppSettingMetadataDictionary(System.Configuration.ApplicationSettingsBase _SettingsCollection)
        {
            PropertyOverridingTypeDescriptor ctd = new PropertyOverridingTypeDescriptor(TypeDescriptor.GetProvider(_SettingsCollection).GetTypeDescriptor(_SettingsCollection));


            // iterate through properies in the supplied object/type
            foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(_SettingsCollection))
            {
                // for every property that complies to our criteria

                AppSettingMetadata asmd;
                bool found = this.TryGetValue(pd.Name, out asmd);

                if (found)
                {
                    // we first construct the custom PropertyDescriptor with the TypeDescriptor's
                    // built-in capabilities
                    Attribute[] attribute_list = new Attribute[2];
                    attribute_list[0] = new CategoryAttribute(asmd.SettingCategory);
                    attribute_list[1] = new DescriptionAttribute(asmd.SettingDescription);


                    PropertyDescriptor pd2 =
                        TypeDescriptor.CreateProperty(
                            _SettingsCollection.GetType(), // or just _settings, if it's already a type
                            pd,                            // base property descriptor to which we want to add attributes
                                   // The PropertyDescriptor which we'll get will just wrap that
                                   // base one returning attributes we need.
                            attribute_list
                            //new CategoryAttribute(asmd.SettingCategory)
                            // this method really can take as many attributes as you like,
                            // not just one
                            );

                    //PropertyDescriptor pd3 =
                    //       TypeDescriptor.CreateProperty(
                    //           _SettingsCollection.GetType(), // or just _settings, if it's already a type
                    //           pd, // base property descriptor to which we want to add attributes
                    //               // The PropertyDescriptor which we'll get will just wrap that
                    //               // base one returning attributes we need.
                    //           new DescriptionAttribute(asmd.SettingDescription)
                    //       // this method really can take as many attributes as you like,
                    //       // not just one
                    //       );

                    // and then we tell our new PropertyOverridingTypeDescriptor to override that property
                    ctd.OverrideProperty(pd2);
                    //ctd.OverrideProperty(pd3);
                }
            }

            // then we add new descriptor provider that will return our descriptor instead of default
            TypeDescriptor.AddProvider(new TypeDescriptorOverridingProvider(ctd), _SettingsCollection);
        }