/// <summary> /// Gets the new value of the cell from a string containing the /// cell's new contents. /// </summary> /// <param name="cell">Cell which has been changed.</param> protected virtual object GetNewPropertyValue(IVariable property, GridCellChangedArgs cell) { if (typeof(IPlant).IsAssignableFrom(property.DataType)) { return(Apsim.Find(property.Object as IModel, cell.NewValue)); } if (property.Display != null && property.Display.Type == DisplayType.Model) { return(Apsim.Get(property.Object as IModel, cell.NewValue)); } try { if (property.DataType.IsEnum) { return(VariableProperty.ParseEnum(property.DataType, cell.NewValue)); } else { return(ReflectionUtilities.StringToObject(property.DataType, cell.NewValue, CultureInfo.CurrentCulture)); } } catch (FormatException err) { throw new Exception($"Value '{cell.NewValue}' is invalid for property '{property.Name}' - {err.Message}."); } }
/// <summary> /// This method takes a value from the grid and formats it appropriately, /// based on the data type of the property to which the value is going to /// be assigned. /// </summary> /// <param name="property">Property to which the value will be assigned.</param> /// <param name="value">Value which is going to be assigned to property.</param> public static object FormatValueForProperty(IVariable property, object value) { if (property.DataType.IsArray && value != null) { string[] stringValues = value.ToString().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (property.DataType == typeof(double[])) { value = MathUtilities.StringsToDoubles(stringValues); } else if (property.DataType == typeof(int[])) { value = MathUtilities.StringsToDoubles(stringValues); } else if (property.DataType == typeof(string[])) { value = stringValues; } else if (property.DataType == typeof(DateTime[])) { value = stringValues.Select(d => DateTime.Parse(d, CultureInfo.InvariantCulture)).ToArray(); } else { throw new ApsimXException(property.Object as IModel, "Invalid property type: " + property.DataType.ToString()); } } else if (typeof(IPlant).IsAssignableFrom(property.DataType)) { value = Apsim.Find(property.Object as IModel, value.ToString()) as IPlant; } else if (property.DataType == typeof(DateTime)) { value = Convert.ToDateTime(value, CultureInfo.InvariantCulture); } else if (property.DataType.IsEnum) { value = VariableProperty.ParseEnum(property.DataType, value.ToString()); } else if (property.Display != null && property.Display.Type == DisplayType.Model) { value = Apsim.Get(property.Object as IModel, value.ToString()); } return(value); }
/// <summary> /// Set the value of the specified property /// </summary> /// <param name="property">The property to set the value of</param> /// <param name="value">The value to set the property to</param> private void SetPropertyValue(IVariable property, object value) { if (property.DataType.IsArray && value != null) { string[] stringValues = value.ToString().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (property.DataType == typeof(double[])) { value = MathUtilities.StringsToDoubles(stringValues); } else if (property.DataType == typeof(int[])) { value = MathUtilities.StringsToDoubles(stringValues); } else if (property.DataType == typeof(string[])) { value = stringValues; } else { throw new ApsimXException(this.model, "Invalid property type: " + property.DataType.ToString()); } } else if (typeof(IPlant).IsAssignableFrom(property.DataType)) { value = Apsim.Find(this.model, value.ToString()) as IPlant; } else if (property.DataType == typeof(DateTime)) { value = Convert.ToDateTime(value); } else if (property.DataType.IsEnum) { value = VariableProperty.ParseEnum(property.DataType, value.ToString()); } else if (property.Display != null && property.Display.Type == DisplayType.Model) { value = Apsim.Get(this.model, value.ToString()); } Commands.ChangeProperty cmd = new Commands.ChangeProperty(this.model, property.Name, value); this.explorerPresenter.CommandHistory.Add(cmd, true); }