Example #1
0
        private bool StoreOption(OptionAttribute attr, PropertyInfo property, IOptions options, bool validateOnly)
        {
            IOptionsDatabase database = mDatabases[attr.Location];

            bool success;

            if (database == null)
            {
                success = false;
            }
            else if (typeof(String).IsAssignableFrom(property.PropertyType))
            {
                success = database.SetStringOption(attr.Path, (string)property.GetValue(options, null), validateOnly);
            }
            else if (typeof(bool).IsAssignableFrom(property.PropertyType))
            {
                success = database.SetBooleanOption(attr.Path, (bool)property.GetValue(options, null), validateOnly);
            }
            else if (typeof(Int32).IsAssignableFrom(property.PropertyType))
            {
                success = database.SetIntegerOption(attr.Path, (int)property.GetValue(options, null), validateOnly);
            }
            else if (typeof(Enum).IsAssignableFrom(property.PropertyType))
            {
                success = database.SetStringOption(attr.Path, property.GetValue(options, null).ToString(), validateOnly);
            }
            else if (typeof(string[]).IsAssignableFrom(property.PropertyType))
            {
                success = database.SetStringArrayOption(attr.Path, (string[])property.GetValue(options, null), validateOnly);
            }
            else if (typeof(Color).IsAssignableFrom(property.PropertyType))
            {
                Color      color      = (Color)property.GetValue(options, null);
                KnownColor knownColor = color.ToKnownColor();
                if (knownColor != 0)
                {
                    success = database.SetStringOption(attr.Path, knownColor.ToString(), validateOnly);
                }
                else
                {
                    success = database.SetStringOption(attr.Path, String.Join(",", new string[] { color.R.ToString(), color.G.ToString(), color.B.ToString() }), validateOnly);
                }
            }
            else
            {
                success = database.SetStringOption(attr.Path, property.GetValue(options, null).ToString(), validateOnly);
            }

            return(success);
        }
Example #2
0
        public void Load(IOptions options)
        {
            foreach (PropertyInfo property in options.GetType().GetProperties())
            {
                object[] attrs = property.GetCustomAttributes(typeof(OptionAttribute), true);
                if (attrs.Length == 1)
                {
                    OptionAttribute  attr     = (OptionAttribute)attrs[0];
                    IOptionsDatabase database = mDatabases[attr.Location];
                    object           value;

                    if (database == null)
                    {
                        value = attr.DefaultValue;
                    }
                    else
                    {
                        if (typeof(String).IsAssignableFrom(property.PropertyType))
                        {
                            value = database.GetStringOption(attr.Path, (string)attr.DefaultValue);
                        }
                        else if (typeof(Boolean).IsAssignableFrom(property.PropertyType))
                        {
                            value = database.GetBooleanOption(attr.Path, (bool)attr.DefaultValue);
                        }
                        else if (typeof(Enum).IsAssignableFrom(property.PropertyType))
                        {
                            try
                            {
                                value = Enum.Parse(property.PropertyType, database.GetStringOption(attr.Path, ""));
                            }
                            catch (Exception)
                            {
                                value = attr.DefaultValue;
                            }
                        }
                        else if (typeof(Int32).IsAssignableFrom(property.PropertyType))
                        {
                            value = database.GetIntegerOption(attr.Path, (int)attr.DefaultValue);
                        }
                        else if (typeof(Color).IsAssignableFrom(property.PropertyType))
                        {
                            try
                            {
                                string name = database.GetStringOption(attr.Path, (string)attr.DefaultValue);
                                if (name.Contains(","))
                                {
                                    string[] components = name.Split(new char[] { ',' });
                                    value = Color.FromArgb(Int32.Parse(components[0]), Int32.Parse(components[1]), Int32.Parse(components[2]));
                                }
                                else
                                {
                                    value = Color.FromName(name);
                                }
                            }
                            catch (System.Exception)
                            {
                                value = attr.DefaultValue;
                            }
                        }
                        else if (typeof(string[]).IsAssignableFrom(property.PropertyType))
                        {
                            value = database.GetStringArrayOption(attr.Path, (string[])attr.DefaultValue);
                        }

                        else
                        {
                            throw new ApplicationException("Don't know how to load option of type: " + property.PropertyType.ToString());
                        }
                    }

                    property.SetValue(options, value, null);
                }
            }
        }