public bool TrySetValue(string value) { if (value == null) { return(false); } string typeName; if (!supportedTypes.TryGetValue(propertyDescriptor.PropertyType, out typeName)) { if (propertyDescriptor.PropertyType.IsEnum) { object enumVal; try { enumVal = Enum.Parse(propertyInfo.PropertyType, value, true); } catch (Exception e) { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj + "\" to " + value + " (not a valid " + propertyInfo.PropertyType + ")", e); return(false); } try { propertyInfo.SetValue(obj, enumVal); } catch (Exception e) { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString(), e); return(false); } } else { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj + "\" to " + value); DebugConsole.ThrowError("(Type not supported)"); return(false); } } try { switch (typeName) { case "bool": propertyInfo.SetValue(obj, value.ToLowerInvariant() == "true", null); break; case "int": int intVal; if (int.TryParse(value, out intVal)) { propertyInfo.SetValue(obj, intVal, null); } break; case "float": float floatVal; if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out floatVal)) { propertyInfo.SetValue(obj, floatVal, null); } break; case "string": propertyInfo.SetValue(obj, value, null); break; case "vector2": propertyInfo.SetValue(obj, XMLExtensions.ParseVector2(value)); break; case "vector3": propertyInfo.SetValue(obj, XMLExtensions.ParseVector3(value)); break; case "vector4": propertyInfo.SetValue(obj, XMLExtensions.ParseVector4(value)); break; case "color": propertyInfo.SetValue(obj, XMLExtensions.ParseColor(value)); break; case "rectangle": propertyInfo.SetValue(obj, XMLExtensions.ParseRect(value, true)); break; } } catch (Exception e) { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString(), e); return(false); } return(true); }
public bool TrySetValue(object value) { if (value == null || obj == null || propertyDescriptor == null) { return(false); } try { string typeName; if (!supportedTypes.TryGetValue(propertyDescriptor.PropertyType, out typeName)) { if (propertyDescriptor.PropertyType.IsEnum) { object enumVal; try { enumVal = Enum.Parse(propertyInfo.PropertyType, value.ToString(), true); } catch (Exception e) { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj + "\" to " + value + " (not a valid " + propertyInfo.PropertyType + ")", e); return(false); } propertyInfo.SetValue(obj, enumVal); return(true); } else { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj + "\" to " + value); DebugConsole.ThrowError("(Type not supported)"); return(false); } } try { if (value.GetType() == typeof(string)) { switch (typeName) { case "string": propertyInfo.SetValue(obj, value, null); return(true); case "vector2": propertyInfo.SetValue(obj, XMLExtensions.ParseVector2((string)value)); return(true); case "vector3": propertyInfo.SetValue(obj, XMLExtensions.ParseVector3((string)value)); return(true); case "vector4": propertyInfo.SetValue(obj, XMLExtensions.ParseVector4((string)value)); return(true); case "color": propertyInfo.SetValue(obj, XMLExtensions.ParseColor((string)value)); return(true); case "rectangle": propertyInfo.SetValue(obj, XMLExtensions.ParseRect((string)value, false)); return(true); default: DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString()); DebugConsole.ThrowError("(Cannot convert a string to a " + propertyDescriptor.PropertyType.ToString() + ")"); return(false); } } else if (propertyDescriptor.PropertyType != value.GetType()) { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString()); DebugConsole.ThrowError("(Non-matching type, should be " + propertyDescriptor.PropertyType + " instead of " + value.GetType() + ")"); return(false); } propertyInfo.SetValue(obj, value, null); } catch (Exception e) { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString(), e); return(false); } return(true); } catch { return(false); } }
public bool TrySetValue(object parentObject, string value) { if (value == null) { return(false); } if (!supportedTypes.TryGetValue(PropertyType, out string typeName)) { if (PropertyType.IsEnum) { object enumVal; try { enumVal = Enum.Parse(propertyInfo.PropertyType, value, true); } catch (Exception e) { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + parentObject + "\" to " + value + " (not a valid " + propertyInfo.PropertyType + ")", e); return(false); } try { propertyInfo.SetValue(parentObject, enumVal); } catch (Exception e) { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + parentObject.ToString() + "\" to " + value.ToString(), e); return(false); } } else { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + parentObject + "\" to " + value); DebugConsole.ThrowError("(Type not supported)"); return(false); } } try { switch (typeName) { case "bool": bool boolValue = value == "true" || value == "True"; if (TrySetValueWithoutReflection(parentObject, boolValue)) { return(true); } propertyInfo.SetValue(parentObject, boolValue, null); break; case "int": if (int.TryParse(value, out int intVal)) { if (TrySetValueWithoutReflection(parentObject, intVal)) { return(true); } propertyInfo.SetValue(parentObject, intVal, null); } else { return(false); } break; case "float": if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out float floatVal)) { if (TrySetValueWithoutReflection(parentObject, floatVal)) { return(true); } propertyInfo.SetValue(parentObject, floatVal, null); } else { return(false); } break; case "string": propertyInfo.SetValue(parentObject, value, null); break; case "point": propertyInfo.SetValue(parentObject, XMLExtensions.ParsePoint(value)); break; case "vector2": propertyInfo.SetValue(parentObject, XMLExtensions.ParseVector2(value)); break; case "vector3": propertyInfo.SetValue(parentObject, XMLExtensions.ParseVector3(value)); break; case "vector4": propertyInfo.SetValue(parentObject, XMLExtensions.ParseVector4(value)); break; case "color": propertyInfo.SetValue(parentObject, XMLExtensions.ParseColor(value)); break; case "rectangle": propertyInfo.SetValue(parentObject, XMLExtensions.ParseRect(value, true)); break; } } catch (Exception e) { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + parentObject.ToString() + "\" to " + value.ToString(), e); return(false); } return(true); }