public bool Validate(IOptions from, IOptions to) { System.Diagnostics.Debug.Assert(to.GetType().IsAssignableFrom(from.GetType())); foreach (PropertyInfo property in from.GetType().GetProperties()) { object[] attrs = property.GetCustomAttributes(typeof(OptionAttribute), true); if (attrs.Length == 1) { OptionAttribute attr = (OptionAttribute)attrs[0]; if (!StoreOption(attr, property, from, true)) { return(false); } } } return(true); }
public OptionsPanel(IManager manager, IOptions options) { InitializeComponent(); mManager = manager; mCurrentOptions = options; mTempOptions = options == null ? null : (IOptions)options.GetType().GetConstructor(new Type[] { }).Invoke(new object[] { }); }
public OptionsPanel(IManager manager, IOptions options) { InitializeComponent(); mManager = manager; mCurrentOptions = options; mTempOptions = options == null ? null : (IOptions)options.GetType().GetConstructor(new Type[] { }).Invoke(new object[] { }); }
public void Store(IOptions options) { foreach (PropertyInfo property in options.GetType().GetProperties()) { object[] attrs = property.GetCustomAttributes(typeof(OptionAttribute), true); if (attrs.Length == 1) { StoreOption((OptionAttribute)attrs[0], property, options, false); } } }
public bool Copy(IOptions from, IOptions to, bool store) { System.Diagnostics.Debug.Assert(to.GetType().IsAssignableFrom(from.GetType())); foreach (PropertyInfo property in from.GetType().GetProperties()) { object[] attrs = property.GetCustomAttributes(typeof(OptionAttribute), true); if (attrs.Length == 1) { OptionAttribute attr = (OptionAttribute)attrs[0]; if (store && !StoreOption(attr, property, from, false)) { return(false); } object value = property.GetValue(from, null); property.SetValue(to, value, null); } } return(true); }
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); } } }