经验证过的跨线程更新辅助类MyInvokeHelper
生活随笔
收集整理的這篇文章主要介紹了
经验证过的跨线程更新辅助类MyInvokeHelper
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; using System.Windows.Forms;namespace Extension.MyDll {/// <summary>/// 輔助類:跨線程更新控件/// </summary>public class MyInvokHelper{#region 委托/// <summary>/// 執行控件對象方法的委托/// </summary>/// <param name="control">控件對象的引用</param>/// <param name="methodName">方法名稱</param>/// <param name="args">方法的參數列表</param>/// <returns></returns>private delegate object MethodInvoker(Control control, string methodName, params object[] args);/// <summary>/// 獲取對象屬性值的委托/// </summary>/// <param name="control">控件對象的引用</param>/// <param name="noneControl">非控件對象的引用</param>/// <param name="propertyName">屬性名稱</param>/// <returns></returns>private delegate object PropertyGetInvoker(Control control, object noneControl, string propertyName);/// <summary>/// 設置對象屬性值的委托/// </summary>/// <param name="control">控件對象的引用</param>/// <param name="noneControl">非控件對象的引用</param>/// <param name="propertyName">屬性名稱</param>/// <param name="value">屬性設置值</param>private delegate void PropertySetInvoker(Control control, object noneControl, string propertyName, object value);#endregion#region 方法/// <summary>/// 獲取對象指定名稱的屬性值/// </summary>/// <param name="control">控件對象的引用</param>/// <param name="noneControl">非控件對象的引用</param>/// <param name="propertyName">屬性名稱</param>/// <returns></returns>private static PropertyInfo GetPropertyInfo(Control control, object noneControl, string propertyName){if ((control != null || noneControl != null) && !string.IsNullOrEmpty(propertyName)){object obj = noneControl != null ? noneControl : control;Type type = obj.GetType();PropertyInfo pi = type.GetProperty(propertyName); //獲取對應名稱的屬性值,未找到返回nullif (pi == null){//未找到對應名稱的屬性throw new InvalidOperationException(string.Format("Can't find property {0} in {1}.", propertyName, type.ToString()));}else{//找到對應名稱的屬性return pi;}}else{throw new ArgumentNullException("Invaild argument.");}}/// <summary>/// 執行控件對象指定名稱的方法(InvokeMethod方法僅針對控件,非控件無Invoke方法)/// </summary>/// <param name="control">控件對象的引用</param>/// <param name="methodName">方法名稱</param>/// <param name="args">方法的參數列表</param>/// <returns></returns>public static object InvokeMethod(Control control, string methodName, params object[] args){if (control != null && !string.IsNullOrEmpty(methodName)){if (control.InvokeRequired){//從創建控件的線程以外訪問控件return control.Invoke(new MethodInvoker(InvokeMethod), new object[] { control, methodName, args });}else{MethodInfo mi = null;bool argsIsNullFlag = true;if (args != null && args.Length > 0){argsIsNullFlag = false;Type[] types = new Type[args.Length];for (int i = 0; i < args.Length; i++){types[i] = args[i] != null ? args[i].GetType() : null;}mi = control.GetType().GetMethod(methodName, types); //匹配帶參方法}else{argsIsNullFlag = true;mi = control.GetType().GetMethod(methodName); //匹配無參方法}if (mi != null){return mi.Invoke(control, argsIsNullFlag ? null : args);}else{throw new InvalidOperationException("Invalid method.");}}}else{throw new ArgumentNullException("Invalid argument.");}}/// <summary>/// 獲取對象指定名稱的屬性值/// </summary>/// <param name="control">控件對象的引用</param>/// <param name="noneControl">非控件對象的引用</param>/// <param name="propertyName">屬性名稱</param>/// <returns></returns>public static object GetProperty(Control control, object noneControl, string propertyName){if ((control != null || noneControl != null) && !string.IsNullOrEmpty(propertyName)){if (control != null && control.InvokeRequired){return control.Invoke(new PropertyGetInvoker(GetProperty), new object[] { control, noneControl, propertyName });}else{PropertyInfo pi = GetPropertyInfo(control, noneControl, propertyName);object invoke = (noneControl == null) ? control : noneControl;if (pi != null){if (pi.CanRead){return pi.GetValue(invoke);}else{throw new FieldAccessException(string.Format("{0}.{1} is a write-only property.", invoke.GetType().ToString(), propertyName));}}else{return null;}}}else{throw new ArgumentNullException("Invalid argument.");}}/// <summary>/// GetProperty方法的重載,獲取控件對象指定名稱的屬性值/// </summary>/// <param name="control">控件對象的引用</param>/// <param name="propertyName">屬性名稱</param>/// <returns></returns>public static object GetProperty(Control control, string propertyName){return GetProperty(control, null, propertyName);}/// <summary>/// 設置對象指定名稱的屬性值/// </summary>/// <param name="control">控件對象的引用</param>/// <param name="noneControl">非控件對象的引用</param>/// <param name="propertyName">屬性名稱</param>/// <param name="value">屬性設置值</param>/// <returns></returns>public static void SetProperty(Control control, object noneControl, string propertyName, object value){if ((control != null || noneControl != null) && !string.IsNullOrEmpty(propertyName)){if (control != null && control.InvokeRequired){control.Invoke(new PropertySetInvoker(SetProperty), new object[] { control, noneControl, propertyName, value });}else{PropertyInfo pi = GetPropertyInfo(control, noneControl, propertyName);object invoke = (noneControl == null) ? control : noneControl;if (pi != null){if (pi.CanWrite){pi.SetValue(invoke, value);}else{throw new FieldAccessException(string.Format("{0}.{1} is a read-only property.", invoke.GetType().ToString(), propertyName));}}}}else{throw new ArgumentNullException("Invalid argument.");}}/// <summary>/// SetProperty方法的重載,設置控件對象指定名稱的屬性值/// </summary>/// <param name="control"></param>/// <param name="propertyName"></param>/// <param name="value"></param>public static void SetProperty(Control control, string propertyName, object value){SetProperty(control, null, propertyName, value);}#endregion} }?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的经验证过的跨线程更新辅助类MyInvokeHelper的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue --- 2.0数据的响应式的一
- 下一篇: 液压系统原理动画_3大类12种液压阀工作