private void SaveToStorage(string propertyName, object value) { if (Table == null) { //TODOLogger.Error("Storage has not be setup for " + this.Name); return; } Type t = this.GetType(); PropertyInfo prop = t.GetProperty(propertyName); if (Attribute.IsDefined(prop, typeof(ConfigTableAttribute))) { ConfigTableAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigTableAttribute)) as ConfigTableAttribute; string ids = string.Empty; Type propertyType = prop.PropertyType; if (typeof(IEnumerable <object>).IsAssignableFrom(propertyType)) { var propValue = value as IEnumerable; if (propValue != null) { foreach (BaseRecord item in propValue) { if (!string.IsNullOrEmpty(ids)) { ids += ","; } ids += item.Id.ToString(); } } } else { ids = (value as BaseRecord).Id.ToString(); } //Find removed ids then delete them from the settings file string[] oldValues = Table.GetTableIDs(propertyName); string[] newValues = ids.Split(','); string[] removedIds = (from o in oldValues where !(from n in newValues where n == o select n).Any() select o).ToArray(); //remove records now they have been found foreach (string id in removedIds) { Table.Database.RemoveTable(id); } //Update/Add records Table.Set(propertyName, ids); } else if (Attribute.IsDefined(prop, typeof(ConfigAttribute)) && !Attribute.IsDefined(prop, typeof(ConfigReadOnlyAttribute))) { Table.Set(propertyName, value); } }
private ObservableCollection <IPostProcessPlugin> GetLookupValues() { PropertyInfo prop = _profile.GetType().GetProperties().Where(p => p.Name == "PostProcessPlugins").FirstOrDefault(); ConfigTableAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigTableAttribute)) as ConfigTableAttribute; Type pluginType = _profile.GetType(); Type propertyType = prop.PropertyType; IEnumerable <Type> availableValues = null; if (configAttribute.RetrieveValuesFrom != null) { var member = pluginType.GetMember(configAttribute.RetrieveValuesFrom); if (member.Length > 0) { if (member[0].MemberType == MemberTypes.Method) { MethodInfo mi = pluginType.GetMethod(configAttribute.RetrieveValuesFrom); var propertyValue = mi.Invoke(_profile, null); availableValues = propertyValue as IEnumerable <Type>; } } } ObservableCollection <IPostProcessPlugin> result = new ObservableCollection <IPostProcessPlugin>(); foreach (Type item in availableValues) { IPostProcessPlugin plugin = Activator.CreateInstance(item) as IPostProcessPlugin; result.Add(plugin); } return(result); }
/// <summary> /// Loads settings from storage /// </summary> public T LoadFromStorage <T>(string propertyName) { T result = default(T); if (Table == null) { //TODO Logger.Error("Storage has not be setup for " + this.Name); return(result); } Type t = this.GetType(); PropertyInfo prop = t.GetProperty(propertyName); if (prop.CanWrite) { if (Attribute.IsDefined(prop, typeof(ConfigLookupAttribute))) { ConfigLookupAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigLookupAttribute)) as ConfigLookupAttribute; Type propertyType = prop.PropertyType; if (propertyType.IsEnum) { string value = Table.GetString(prop.Name); if (!string.IsNullOrEmpty(value)) { result = (T)(Enum.Parse(propertyType, value) as Object); } } else if (prop.PropertyType == typeof(int?)) { result = (T)(Table.GetInt(prop.Name) as object); } else if (prop.PropertyType == typeof(string)) { result = (T)(Table.GetString(prop.Name) as object); } else { //TODO change to log error throw new Exception(prop.PropertyType.ToString() + " is not supported update BasePlugin.LoadFromStorage() to implement support"); } } else if (Attribute.IsDefined(prop, typeof(ConfigNumberAttribute))) { ConfigNumberAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigNumberAttribute)) as ConfigNumberAttribute; if (prop.PropertyType == typeof(int?)) { result = (T)(Table.GetInt(prop.Name) as object); } else if (prop.PropertyType == typeof(double?)) { result = (T)(Table.GetDouble(prop.Name) as object); } else { throw new Exception(prop.PropertyType.ToString() + " is not supported update BasePlugin.LoadFromStorage() to implement support"); } } else if (Attribute.IsDefined(prop, typeof(ConfigStringAttribute))) { ConfigStringAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigStringAttribute)) as ConfigStringAttribute; result = (T)(Table.GetString(prop.Name) as object); } else if (Attribute.IsDefined(prop, typeof(ConfigTableAttribute))) { ConfigTableAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigTableAttribute)) as ConfigTableAttribute; //Single Item if (typeof(T).IsInterface) { ITable table = Table.GetTables(prop.Name).FirstOrDefault(); if (table != null) { string tableTypeString = table.GetString("Type"); if (!string.IsNullOrEmpty(tableTypeString)) { //Get type from local project if possible, if not use other loader Type tableType = System.Type.GetType(tableTypeString) ?? Runtime.Loader.GetObjectType(tableTypeString); string id = table.GetString("Id"); BaseRecord createdTable = Activator.CreateInstance(tableType, Table.Database.AddTable(id), Logger, this.Runtime) as BaseRecord; result = (T)(createdTable as object); } } } //List of items else { result = (T)Activator.CreateInstance(typeof(T)); foreach (ITable table in Table.GetTables(prop.Name)) { string tableTypeString = table.GetString("Type"); if (!string.IsNullOrEmpty(tableTypeString)) { //Get type from local project if possible, if not use other loader Type tableType = System.Type.GetType(tableTypeString) ?? Runtime.Loader.GetObjectType(tableTypeString); string id = table.GetString("Id"); try { BaseRecord createdTable = Activator.CreateInstance(tableType, Table.Database.AddTable(id), Logger, this.Runtime) as BaseRecord; result.GetType().GetMethod("Add").Invoke(result, new object[] { createdTable }); } catch { // TODO: There is a frequent error here when loading lights plugin where Afterglow.ini is already in use. } } } } } } return(result); }