Mike Podonyi avatar Mike Podonyi committed e4ec276

work on Settingsproperties discovery

Comments (0)

Files changed (1)

src/Empi.Camino/Configuration/Settings.cs

 using Empi.Camino.Web.Data;
 using System.Reflection;
 using System.Linq;
+using System.ComponentModel;
 
 namespace Empi.Camino.Configuration
 {
-	//TODO: maybe inherit from ApplicationSettingsBase class
-	[SettingsProvider(typeof(Empi.Camino.Configuration.CaminoSettingsProvider))]
 	internal abstract class Settings : SettingsBase
 	{
 		private static readonly CaminoSectionGroup CaminoSettings = ((System.Web.HttpContext.Current == null) ? ConfigurationManager.OpenExeConfiguration(null).GetSectionGroup("empi.camino") : WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath).GetSectionGroup("empi.camino")) as CaminoSectionGroup;
             get;
         }
 
+
+        private SettingsPropertyCollection _Properties = null;
         public override SettingsPropertyCollection Properties
         {
             get
             {
-
-                //MP: this what is in the moment in this... copy to here
-                return base.Properties;
+                return (_Properties == null) ? _Properties = CreateSettings() : _Properties;
             }
         }
 
 			}
 		}
 
+        private SettingsPropertyCollection CreateSettings()
+        {
+            SettingsPropertyCollection coll = new SettingsPropertyCollection();
+            foreach(PropertyInfo prop in GetSettingsProperties())
+            {
+                coll.Add(CreateSetting(prop));
+            }
+
+            return coll;
+        }
+
+        private SettingsProperty CreateSetting(PropertyInfo propInfo)
+        {
+            object[] customAttributes = propInfo.GetCustomAttributes(false);
+            SettingsProperty property = new SettingsProperty(this.SettingsPropertyPrototype);
+            bool flag = false;
+           
+            property.PropertyType = propInfo.PropertyType;
+            for (int i = 0; i < customAttributes.Length; i++)
+            {
+                Attribute attribute = customAttributes[i] as Attribute;
+                if (attribute is DefaultSettingValueAttribute)
+                {
+                    property.DefaultValue = ((DefaultSettingValueAttribute)attribute).Value;
+                }
+                else if (attribute is ReadOnlyAttribute)
+                {
+                    property.IsReadOnly = true;
+                }
+                else if (attribute is CaminoSettingsAttribute)
+                {
+                    property.Name = ((CaminoSettingsAttribute)attribute).Key;
+                }
+                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;
+        }
+
+        private SettingsSerializeAs GetSerializeAs(Type type)
+        {
+            TypeConverter converter = TypeDescriptor.GetConverter(type);
+            bool flag = converter.CanConvertTo(typeof(string));
+            bool flag2 = converter.CanConvertFrom(typeof(string));
+            if (flag && flag2)
+            {
+                return SettingsSerializeAs.String;
+            }
+            return SettingsSerializeAs.Xml;
+        }
+
+        private SettingsProperty _init = null;
+        private SettingsProperty SettingsPropertyPrototype
+        {
+            get
+            {
+                if (this._init == null)
+                {
+                    this._init = new SettingsProperty("");
+                    this._init.DefaultValue = null;
+                    this._init.IsReadOnly = false;
+                    this._init.PropertyType = null;
+                    this._init.Provider = this.Providers["CaminoSettingsProvider"];
+                }
+                return this._init;
+            }
+        }
+
         private PropertyInfo[] GetSettingsProperties()
         {
             return (from obj in this.GetType().GetProperties(BindingFlags.Instance)
             }
             set
             {
-                if (Properties[propertyName] == null)
+                lock (this)
                 {
-                    SettingsProperty property = new SettingsProperty(propertyName,
-                        typeof(string),
-                        this.Providers["CaminoSettingsProvider"],
-                        false,
-                        string.Empty,
-                        SettingsSerializeAs.String,
-                        null,
-                        true,
-                        true);
-
-
-
-
-                    Properties.Add(property);
+                    base[propertyName] = value;
+                    this.Save();
                 }
-
-                base[propertyName] = value;
             }
         }
 	}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.