public static object GetPropertyInstance(Control ctrl, string propertyName) { if (ctrl.InvokeRequired) { GetPropertyInstanceDelegate del = new GetPropertyInstanceDelegate(CrossThreadUI.GetPropertyInstance); if (CrossThreadUI.ExecSync) { return(ctrl.Invoke(del, ctrl, propertyName)); } else { ctrl.BeginInvoke(del, ctrl, propertyName); } } else { Type ctrlType = ctrl.GetType(); PropertyInfo pi = ctrlType.GetProperty(propertyName, CrossThreadUI.Binding); if (pi != null) { return(pi.GetValue(ctrl, null)); } else { throw new ArgumentException("Specified control does not expose a '" + propertyName + "' property."); } } return(null); }
/// <summary> /// Gets the object instance assigned to the property of a given control. /// </summary> /// <param name="ctrl">The System.Windows.Forms.Control instance whose property will be queried.</param> /// <param name="propertyName">The name of the property whose value will be returned.</param> /// <returns>An object whose type will match the specified property's signature.</returns> public static object GetPropertyInstance(Control ctrl, string propertyName) { if (ctrl.InvokeRequired) { GetPropertyInstanceDelegate del = new GetPropertyInstanceDelegate(CrossThreadUI.GetPropertyInstance); if (CrossThreadUI.ExecSync) return ctrl.Invoke(del, ctrl, propertyName); else ctrl.BeginInvoke(del, ctrl, propertyName); } else { Type ctrlType = ctrl.GetType(); PropertyInfo pi = ctrlType.GetProperty(propertyName, CrossThreadUI.Binding); if (pi != null) { return pi.GetValue(ctrl, null); } else throw new ArgumentException("Specified control does not expose a '" + propertyName + "' property."); } return null; }