private void ApplyToProperty(ObjectProperty property, object value, float deltaTime) { if (disableDeltaTime || setValue) { deltaTime = 1.0f; } Type type = value.GetType(); if (type == typeof(float) || (type == typeof(int) && property.GetValue() is float)) { float floatValue = Convert.ToSingle(value) * deltaTime; if (!setValue) { floatValue += (float)property.GetValue(); } property.TrySetValue(floatValue); } else if (type == typeof(int) && value is int) { int intValue = (int)((int)value * deltaTime); if (!setValue) { intValue += (int)property.GetValue(); } property.TrySetValue(intValue); } else if (type == typeof(bool) && value is bool) { property.TrySetValue((bool)value); } else if (type == typeof(string)) { property.TrySetValue((string)value); } else { DebugConsole.ThrowError("Couldn't apply value " + value.ToString() + " (" + type + ") to property \"" + property.Name + "\" (" + property.GetValue().GetType() + ")! " + "Make sure the type of the value set in the config files matches the type of the property."); } }
public static Dictionary <string, ObjectProperty> InitProperties(IPropertyObject obj, XElement element) { var properties = TypeDescriptor.GetProperties(obj.GetType()).Cast <PropertyDescriptor>(); Dictionary <string, ObjectProperty> dictionary = new Dictionary <string, ObjectProperty>(); foreach (var property in properties) { ObjectProperty objProperty = new ObjectProperty(property, obj); dictionary.Add(property.Name.ToLowerInvariant(), objProperty); //set the value of the property to the default value if there is one foreach (var ini in property.Attributes.OfType <HasDefaultValue>()) { objProperty.TrySetValue(ini.defaultValue); break; } } if (element != null) { //go through all the attributes in the xml element //and set the value of the matching property if it is initializable foreach (XAttribute attribute in element.Attributes()) { ObjectProperty property = null; if (!dictionary.TryGetValue(attribute.Name.ToString().ToLowerInvariant(), out property)) { continue; } if (!property.Attributes.OfType <HasDefaultValue>().Any()) { continue; } property.TrySetValue(attribute.Value); } } return(dictionary); }