private static InnerParametrizedObject GetInnerParametrizedObject(
            IParametrizedObject @this,
            MemberInfo fieldOrProperty)
        {
            FieldInfo fi = fieldOrProperty as FieldInfo;

            InnerParametrizedObject result = new InnerParametrizedObject()
            {
                ParentFieldName = fieldOrProperty.Name
            };

            if (fi != null)
            {
                result.ParametrizedObject = fi.GetValue(@this) as IParametrizedObject;
                return(result);
            }

            PropertyInfo pi = fieldOrProperty as PropertyInfo;

            if (pi != null)
            {
                result.ParametrizedObject = pi.GetValue(@this, null) as IParametrizedObject;
                return(result);
            }

            return(null);
        }
        private static Parameter CreateParameter(IParametrizedObject @this, MemberInfo fieldOrProperty, object attribute)
        {
            Parameter result = null;

            ParameterAttribute parameterAttribute = attribute as ParameterAttribute;

            Type fieldType = fieldOrProperty.GetFieldOrPropertyType();

            string typeDescription = GetFieldOrPropertyTypeDescription(@this, fieldType, fieldOrProperty);

            result = new Parameter(
                @this,
                fieldOrProperty,
                fieldType,
                parameterAttribute.MinimumValue,
                parameterAttribute.MaximumValue,
                parameterAttribute.StringType,
                parameterAttribute.Description,
                typeDescription);

            if (!result.IsString && result.StringType != StringParameterType.Unspecified)
            {
                throw new InvalidOperationException("'StringType' argument can be used only with string fields. Class: " + @this.GetType().FullName + ", field name: " + fieldOrProperty.Name);
            }

            if (!result.IsNumeric && (result.MinimumValue != null || result.MaximumValue != null))
            {
                throw new InvalidOperationException("'MinimumValue' and 'MaximumValue' arguments can be used only with numeric fields. Class: " + @this.GetType().FullName + ", field name: " + fieldOrProperty.Name);
            }

            return(result);
        }
        private void ConfigureObjectWithInnerObjects(IParametrizedObject parametrizedObject)
        {
            if (this.inhibitParameterWindow)
            {
                return;
            }

            ParameterWindow.ConfigureParametrizedObjectAndInnerObjects(parametrizedObject);
        }
        private void ConfigureObject(IParametrizedObject parametrizedObject)
        {
            if (this.inhibitParameterWindow)
            {
                return;
            }

            ParameterWindow.ConfigureParametrizedObject(parametrizedObject);
        }
        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());
            }
        }
        public static ParametersSnapshot GetParameterSnapshotWithInnerObjects(this IParametrizedObject @this, string parentFieldName)
        {
            ParametersSnapshot result = @this.GetParametersSnapshot(parentFieldName);

            result.InnerSnapshots = @this
                                    .GetInnerParametrizedObjects()
                                    .Select(io => GetParameterSnapshotWithInnerObjects(io.ParametrizedObject, io.ParentFieldName))
                                    .ToArray();

            return(result);
        }
 public static IEnumerable <Parameter> GetParameters(this IParametrizedObject @this)
 {
     return(@this
            .GetType()
            .GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
            .Concat <MemberInfo>(@this.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
            .Select(fop => new { FieldOrProperty = fop, Attribute = ProcessAttributes(fop) })
            .Where(d => d.Attribute != null)
            .Select(d => CreateParameter(@this, d.FieldOrProperty, d.Attribute))
            .ToArray());
 }
        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);
        }
 public static IEnumerable <InnerParametrizedObject> GetInnerParametrizedObjects(
     this IParametrizedObject @this)
 {
     return(@this
            .GetType()
            .GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
            .Concat <MemberInfo>(@this.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
            .Where(fop => !fop.GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Any())
            .Where(fop => typeof(IParametrizedObject).IsAssignableFrom(fop.GetFieldOrPropertyType()))
            .Select(fop => GetInnerParametrizedObject(@this, fop))
            .Where(po => po != null)
            .ToArray());
 }
        private static string GetFieldOrPropertyTypeDescription(IParametrizedObject @this, Type type, MemberInfo fieldOrProperty)
        {
            if (fieldTypeToDescription.ContainsKey(type))
            {
                return(fieldTypeToDescription[type]);
            }

            if (type.BaseType != null && fieldTypeToDescription.ContainsKey(type.BaseType))
            {
                return(fieldTypeToDescription[type.BaseType]);
            }

            throw new InvalidOperationException("Unsupported field type: '" + type.FullName + "' used in class: '" + @this.GetType().FullName + "', field name: '" + fieldOrProperty.Name + "'");
        }
        public static void CopyParametersFromWithInnerObjects(this IParametrizedObject @this, IParametrizedObject other)
        {
            Contract.Requires(@this.GetType().Equals(other.GetType()));

            @this.CopyParametersFrom(other);

            foreach (var objectPair
                     in Enumerable.Zip(@this.GetInnerParametrizedObjects(), other.GetInnerParametrizedObjects(), (o1, o2) => new { O1 = o1, O2 = o2 }))
            {
                objectPair.O1.ParametrizedObject.CopyParametersFromWithInnerObjects(objectPair.O2.ParametrizedObject);
            }

            @this.ParametersChanged();
        }
        public static void SetParametersFromSnapshotWithInnerObjects(this IParametrizedObject @this, ParametersSnapshot snapshot)
        {
            @this.SetParametersFromSnapshot(snapshot);

            Dictionary <string, ParametersSnapshot> snapshots = snapshot.InnerSnapshots.ToDictionary(s => s.ParentFieldName, s => s);

            foreach (InnerParametrizedObject io in @this.GetInnerParametrizedObjects())
            {
                if (snapshots.ContainsKey(io.ParentFieldName))
                {
                    io.ParametrizedObject.SetParametersFromSnapshotWithInnerObjects(snapshots[io.ParentFieldName]);
                }
            }

            @this.ParametersChanged();
        }
        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();
        }
Exemplo n.º 15
0
 public static DialogResult ConfigureParametrizedObject(IParametrizedObject parametrizedObject)
 {
     return((new ParameterWindow(parametrizedObject, false)).ShowDialog());
 }
Exemplo n.º 16
0
 public static DialogResult ConfigureParametrizedObjectAndInnerObjects(IParametrizedObject parametrizedObject)
 {
     return((new ParameterWindow(parametrizedObject, true)).ShowDialog());
 }
Exemplo n.º 17
0
        public void Initialize(IParametrizedObject parametrizedObject, bool includeInnerObjectParameters)
        {
            this.panel.Controls.Clear();
            this.innerParameterControls.Clear();

            this.parametrizedObject = parametrizedObject;

            this.panel.SuspendLayout();
            this.SuspendLayout();

            AddTitleLabel();

            IEnumerable <Parameter> parameters = this.parametrizedObject.GetParameters();
            IEnumerable <InnerParametrizedObject> innerParametrizedObjects = this.parametrizedObject.GetInnerParametrizedObjects();

            if (parameters.Any())
            {
                foreach (var parameter in parameters)
                {
                    FlowLayoutPanel panel = new FlowLayoutPanel();
                    panel.CausesValidation = true;
                    panel.FlowDirection    = FlowDirection.LeftToRight;
                    panel.WrapContents     = false;
                    panel.AutoSize         = true;
                    panel.AutoScroll       = false;
                    panel.AutoSize         = true;

                    AddParameterNameLabel(parameter, panel);
                    AddParameterControl(parameter, panel);
                    AddParameterDescriptionLabel(parameter, panel);

                    this.panel.Controls.Add(panel);
                }
            }
            else
            {
                Label noParameterInfoLabel = new Label();
                noParameterInfoLabel.AutoSize  = true;
                noParameterInfoLabel.Text      = "This object has no parameters";
                noParameterInfoLabel.Enabled   = false;
                noParameterInfoLabel.Font      = new Font(noParameterInfoLabel.Font, FontStyle.Italic);
                noParameterInfoLabel.TextAlign = ContentAlignment.MiddleCenter;
                noParameterInfoLabel.Margin    = new Padding(5);
                this.panel.Controls.Add(noParameterInfoLabel);
            }

            if (includeInnerObjectParameters)
            {
                foreach (InnerParametrizedObject innerParametrizedObject in innerParametrizedObjects)
                {
                    ParameterControl pc = new ParameterControl();
                    pc.ParameterValueEdited    = this.ParameterValueEdited;
                    pc.AutoSize                = true;
                    pc.OwnerComponentName      = this.parametrizedObject.GetType().GetDisplayName();
                    pc.OwnerComponentFieldName = innerParametrizedObject.ParentFieldName;
                    pc.Initialize(innerParametrizedObject.ParametrizedObject, true);

                    this.panel.Controls.Add(pc);
                    this.innerParameterControls.Add(pc);
                }
            }

            this.ResumeLayout();
            this.panel.ResumeLayout();
            this.panel.PerformLayout();
        }
 public static ParametersSnapshot GetParameterSnapshotWithInnerObjects(this IParametrizedObject @this)
 {
     return(@this.GetParameterSnapshotWithInnerObjects(string.Empty));
 }
Exemplo n.º 19
0
        public ParameterWindow(IParametrizedObject parametrizedObject, bool withInnerObjects)
        {
            InitializeComponent();

            this.parameterControl.Initialize(parametrizedObject, withInnerObjects);
        }