public static void CopyParametersFrom(this IParametrizedObject @this, IParametrizedObject other) { Contract.Requires(@this.GetType().Equals(other.GetType())); foreach (var parameterPair in Enumerable.Zip(@this.GetParameters(), other.GetParameters(), (p1, p2) => new { P1 = p1, P2 = p2 })) { parameterPair.P1.SetValue(parameterPair.P2.GetValue()); } }
private void ConfigureItemClick(object sender, EventArgs e) { IParametrizedObject parametrizedObject = this.gridViewMenuStrip.Tag as IParametrizedObject; if (!parametrizedObject.GetParameters().Any()) { MessageBox.Show("This item has no configuration parameters", "Nothing to configure", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } ConfigureObjectWithInnerObjects(parametrizedObject); }
private static ParametersSnapshot GetParametersSnapshot(this IParametrizedObject @this, string parentFieldName) { ParametersSnapshot result = new ParametersSnapshot() { ParentFieldName = parentFieldName }; result.ParameterValues = @this .GetParameters() .Select(p => new ParameterValuePair() { Name = p.Field.Name, Value = (string)Convert.ChangeType(p.GetValue(), typeof(string), CultureInfo.InvariantCulture) }) .ToArray(); return(result); }
public static void SetParametersFromSnapshot(this IParametrizedObject @this, ParametersSnapshot snapshot) { Dictionary <string, string> parameterValues = snapshot.ParameterValues.ToDictionary(pvp => pvp.Name, pvp => pvp.Value); foreach (Parameter parameter in @this.GetParameters()) { if (parameterValues.ContainsKey(parameter.Field.Name)) { if (parameter.IsEnum) { parameter.SetValue(Enum.Parse(parameter.FieldType, parameterValues[parameter.Field.Name])); } else { parameter.SetValue(Convert.ChangeType(parameterValues[parameter.Field.Name], parameter.FieldType, CultureInfo.InvariantCulture)); } } } @this.ParametersChanged(); }