/// <summary> /// gets the SettingAttribute of a property or field /// </summary> /// <param name="parentClass">the Type of the class which contains the object with the attribute</param> /// <param name="attributeName">the name which is defined in the Attribute definition</param> /// <returns>The SettingAttribute of the object. null if the atttribute was not found</returns> public static SettingAttribute GetAttribute(Type parentClass, string attributeName) { IEnumerable <FieldInfo> fieldInfos = parentClass.GetFields().Where(field => field.IsDefined(typeof(SettingAttribute), false)); foreach (FieldInfo fieldInfo in fieldInfos) { SettingAttribute attr = (SettingAttribute)fieldInfo.GetCustomAttributes(typeof(SettingAttribute), false).First(); if (attr.Name == attributeName) { return(attr); } } IEnumerable <PropertyInfo> propertyInfos = parentClass.GetProperties().Where(prop => prop.IsDefined(typeof(SettingAttribute), false)); foreach (PropertyInfo propertyInfo in propertyInfos) { SettingAttribute attr = (SettingAttribute)propertyInfo.GetCustomAttributes(typeof(SettingAttribute), false).First(); if (attr.Name == attributeName) { return(attr); } } return(null); }
/// <summary> /// Saves all Settings which were changed temporary /// </summary> /// <param name="only">the name of the containSettingsAttribute which shall be saved</param> public static void SaveTemporaryChanges(string only = "") { if (_changeBufferDictionary.Count == 0) { return; } foreach (KeyValuePair <string[], object> keyValuePair in _changeBufferDictionary) { if (only.Length != 0 && keyValuePair.Key.First() != only) { continue; } object parent = null; foreach (ContainSettingObject containSettingObject in SettingList) { if (containSettingObject.Name == keyValuePair.Key.First()) { parent = containSettingObject.Obj; break; } } if (parent == null) { return; } for (int i = 1; i < keyValuePair.Key.Length - 1; i++) { bool parentFound = false; IEnumerable <FieldInfo> fieldInfos = parent.GetType().GetFields().Where(prop => prop.IsDefined(typeof(SettingMenuItemAttribute), false)); foreach (FieldInfo fieldInfo in fieldInfos) { SettingMenuItemAttribute attr = (SettingMenuItemAttribute) fieldInfo.GetCustomAttributes(typeof(SettingMenuItemAttribute), false).First(); if (attr.Name == keyValuePair.Key[i]) { parent = fieldInfo.GetValue(parent); parentFound = true; break; } } if (!parentFound) { IEnumerable <PropertyInfo> propertyInfos = parent.GetType() .GetProperties() .Where(prop => prop.IsDefined(typeof(SettingMenuItemAttribute), false)); foreach (PropertyInfo propertyInfo in propertyInfos) { SettingMenuItemAttribute attr = (SettingMenuItemAttribute) propertyInfo.GetCustomAttributes(typeof(SettingMenuItemAttribute), false).First(); if (attr.Name == keyValuePair.Key[i]) { parent = propertyInfo.GetValue(parent, null); break; } } } } //parent found; get value bool valueSet = false; IEnumerable <FieldInfo> fieldInfos2 = parent.GetType().GetFields().Where(prop => prop.IsDefined(typeof(SettingAttribute), false)); foreach (FieldInfo fieldInfo in fieldInfos2) { SettingAttribute attr = (SettingAttribute)fieldInfo.GetCustomAttributes(typeof(SettingAttribute), false).First(); if (attr.Name == keyValuePair.Key.Last()) { object value; if (keyValuePair.Value.GetType().IsArray) { object[] values = ((IEnumerable)keyValuePair.Value).Cast <object>().ToArray(); //cannot cast directly from object to object[] //convert object[] to an array of the correct type to avoid contravariance Type type = fieldInfo.FieldType.GetElementType(); Array typeSpecificArray = Array.CreateInstance(type, values.Length); Array.Copy(values, typeSpecificArray, values.Length); value = typeSpecificArray; } else { value = keyValuePair.Value; } fieldInfo.SetValue(parent, value); valueSet = true; break; } } if (!valueSet) { IEnumerable <PropertyInfo> propertyInfos2 = parent.GetType().GetProperties().Where(prop => prop.IsDefined(typeof(SettingAttribute), false)); foreach (PropertyInfo propertyInfo in propertyInfos2) { //propertyInfo.PropertyType object value; if (keyValuePair.Value.GetType().IsArray) { object[] values = ((IEnumerable)keyValuePair.Value).Cast <object>().ToArray(); //cannot cast directly from object to object[] //convert object[] to an array of the correct type to avoid contravariance Type type = propertyInfo.PropertyType.GetElementType(); Array typeSpecificArray = Array.CreateInstance(type, values.Length); Array.Copy(values, typeSpecificArray, values.Length); value = typeSpecificArray; } else { value = keyValuePair.Value; } SettingAttribute attr = (SettingAttribute)propertyInfo.GetCustomAttributes(typeof(SettingAttribute), false).First(); if (attr.Name == keyValuePair.Key.Last()) { propertyInfo.SetValue(parent, value, null); break; } } } } _changeBufferDictionary.Clear(); }