コード例 #1
0
        /// <summary>
        /// Invokes a method on the specified property instance of a given control.
        /// </summary>
        /// <param name="ctrl">The control containing the property returning an object instance.</param>
        /// <param name="propertyName">The name of the property on the control.</param>
        /// <param name="methodName">
        /// The name of the method to be invoked.  This will be queried from the resulting Type of
        /// the instance object returned by the specified property.
        /// </param>
        /// <param name="args">Any arguments the method requires.  Array objects must be properly typed to match the method's signature.</param>
        /// <returns>An object whose type will match the specified method's signature or null if the method has no return value.</returns>
        public static object InvokePropertyMethod(Control ctrl, string propertyName, string methodName, params object[] args)
        {
            if (ctrl.InvokeRequired)
            {
                InvokePropertyMethodDelegate del = new InvokePropertyMethodDelegate(CrossThreadUI.InvokePropertyMethod);
                if (CrossThreadUI.ExecSync)
                {
                    ctrl.Invoke(del, ctrl, propertyName, methodName, args);
                }
                else
                {
                    ctrl.BeginInvoke(del, ctrl, propertyName, methodName, args);
                }
            }
            else
            {
                object propVal = CrossThreadUI.GetPropertyInstance(ctrl, propertyName);
                if (propVal != null)
                {
                    Type objType = propVal.GetType();

                    // Determine the type of each passed argument in order to try and
                    //   determine the unique signature for the requested method.
                    Type[] paramTypes = new Type[args.Length];
                    for (int i = 0; i < paramTypes.Length; i++)
                    {
                        paramTypes[i] = args[i].GetType();
                    }

                    // Now, it's time to get a reference to the method.
                    MethodInfo mi = objType.GetMethod(methodName, paramTypes);
                    if (mi != null)
                    {
                        return(mi.Invoke(propVal, args));
                    }
                    else
                    {
                        throw new ArgumentException("Specified ('" + propertyName + "') property value does not expose a '" + methodName + "' method with the provided parameter types.");
                    }
                }
            }
            return(null);
        }
コード例 #2
0
 /// <summary>
 /// Sets the 'Value' property of a given control.  An exception will be thrown if the control does not expose a property called 'Value'.
 /// </summary>
 /// <param name="ctrl">The System.Windows.Forms.Control object whose Value property will be set.</param>
 /// <param name="value">The System.Object value to assign to the property.</param>
 public static void SetValue(Control ctrl, object value)
 {
     try
     { CrossThreadUI.SetPropertyValue(ctrl, "Value", value); }
     catch { throw; }
 }
コード例 #3
0
 /// <summary>
 /// Sets the 'Checked' property of a given control.  An exception will be thrown if the control does not expose a property called 'Checked'.
 /// </summary>
 /// <param name="ctrl">The System.Windows.Forms.Control object whose checked property will be set.</param>
 /// <param name="value">The Sytem.Boolean value to assign to the property.</param>
 public static void SetChecked(Control ctrl, bool value)
 {
     try
     { CrossThreadUI.SetPropertyValue(ctrl, "Checked", value); }
     catch { throw; }
 }