public bool TrySetValue(object parentObject, object value) { if (value == null || parentObject == null || propertyInfo == null) { return(false); } try { if (!supportedTypes.TryGetValue(PropertyType, out string typeName)) { if (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 \"" + parentObject + "\" to " + value + " (not a valid " + propertyInfo.PropertyType + ")", e); return(false); } propertyInfo.SetValue(parentObject, enumVal); return(true); } else { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + parentObject + "\" to " + value); DebugConsole.ThrowError("(Type not supported)"); return(false); } } try { if (value.GetType() == typeof(string)) { switch (typeName) { case "string": propertyInfo.SetValue(parentObject, value, null); return(true); case "point": propertyInfo.SetValue(parentObject, XMLExtensions.ParsePoint((string)value)); return(true); case "vector2": propertyInfo.SetValue(parentObject, XMLExtensions.ParseVector2((string)value)); return(true); case "vector3": propertyInfo.SetValue(parentObject, XMLExtensions.ParseVector3((string)value)); return(true); case "vector4": propertyInfo.SetValue(parentObject, XMLExtensions.ParseVector4((string)value)); return(true); case "color": propertyInfo.SetValue(parentObject, XMLExtensions.ParseColor((string)value)); return(true); case "rectangle": propertyInfo.SetValue(parentObject, XMLExtensions.ParseRect((string)value, false)); return(true); default: DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + parentObject.ToString() + "\" to " + value.ToString()); DebugConsole.ThrowError("(Cannot convert a string to a " + PropertyType.ToString() + ")"); return(false); } } else if (PropertyType != value.GetType()) { DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + parentObject.ToString() + "\" to " + value.ToString()); DebugConsole.ThrowError("(Non-matching type, should be " + PropertyType + " instead of " + value.GetType() + ")"); return(false); } propertyInfo.SetValue(parentObject, value, null); } 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); } catch (Exception e) { DebugConsole.ThrowError("Error in SerializableProperty.TrySetValue", e); 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); }