Exemple #1
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);
                }
            }
        }