InvokeHelper:多線程修改主介面控制項屬性並調用其中方法

來源:互聯網
上載者:User

標籤:tostring   style   help   gen   string   new   tty   ref   -o   

挺不錯的方法,先網摘過來留個記號http://blog.csdn.net/conmajia/article/details/7831251

 

/******************************************************************************* * InvokeHelper.cs * A thread-safe control invoker helper class. * ----------------------------------------------------------------------------- * Project:Conmajia.Controls * Author:Conmajia * Url:[email protected] * History: *      4th Aug., 2012 *      Added support for "Non-control" controls (such as ToolStripItem). *       *      4th Aug., 2012 *      Initiated. ******************************************************************************/using System;using System.Collections.Generic;using System.Reflection;using System.Text;using System.Windows.Forms;namespace InvokerHelperDemo{    /// <summary>    /// A thread-safe control invoker helper class.    /// </summary>    public class InvokeHelper    {        #region delegates        private delegate object MethodInvoker(Control control, string methodName, params object[] args);        private delegate object PropertyGetInvoker(Control control, object noncontrol, string propertyName);        private delegate void PropertySetInvoker(Control control, object noncontrol, string propertyName, object value);        #endregion        #region static methods        // helpers        private static PropertyInfo GetPropertyInfo(Control control, object noncontrol, string propertyName)        {            if (control != null && !string.IsNullOrEmpty(propertyName))            {                PropertyInfo pi = null;                Type t = null;                if (noncontrol != null)                    t = noncontrol.GetType();                else                    t = control.GetType();                pi = t.GetProperty(propertyName);                if (pi == null)                    throw new InvalidOperationException(                        string.Format(                        "Can‘t find property {0} in {1}.",                        propertyName,                        t.ToString()                        ));                return pi;            }            else                throw new ArgumentNullException("Invalid argument.");        }        // outlines        public static object Invoke(Control control, string methodName, params object[] args)        {            if (control != null && !string.IsNullOrEmpty(methodName))                if (control.InvokeRequired)                    return control.Invoke(                        new MethodInvoker(Invoke),                        control,                        methodName,                        args                        );                else                {                    MethodInfo mi = null;                    if (args != null && args.Length > 0)                    {                        Type[] types = new Type[args.Length];                        for (int i = 0; i < args.Length; i++)                        {                            if (args[i] != null)                                types[i] = args[i].GetType();                        }                        mi = control.GetType().GetMethod(methodName, types);                    }                    else                        mi = control.GetType().GetMethod(methodName);                    // check method info you get                    if (mi != null)                        return mi.Invoke(control, args);                    else                        throw new InvalidOperationException("Invalid method.");                }            else                throw new ArgumentNullException("Invalid argument.");        }        public static object Get(Control control, string propertyName)        {            return Get(control, null, propertyName);        }        public static object Get(Control control, object noncontrol, string propertyName)        {            if (control != null && !string.IsNullOrEmpty(propertyName))                if (control.InvokeRequired)                    return control.Invoke(new PropertyGetInvoker(Get),                        control,                        noncontrol,                        propertyName                        );                else                {                    PropertyInfo pi = GetPropertyInfo(control, noncontrol, propertyName);                    object invokee = (noncontrol == null) ? control : noncontrol;                    if (pi != null)                        if (pi.CanRead)                            return pi.GetValue(invokee, null);                        else                            throw new FieldAccessException(                                string.Format(                                "{0}.{1} is a write-only property.",                                invokee.GetType().ToString(),                                propertyName                                ));                    return null;                }            else                throw new ArgumentNullException("Invalid argument.");        }        public static void Set(Control control, string propertyName, object value)        {            Set(control, null, propertyName, value);        }        public static void Set(Control control, object noncontrol, string propertyName, object value)        {            if (control != null && !string.IsNullOrEmpty(propertyName))                if (control.InvokeRequired)                    control.Invoke(new PropertySetInvoker(Set),                        control,                        noncontrol,                        propertyName,                        value                        );                else                {                    PropertyInfo pi = GetPropertyInfo(control, noncontrol, propertyName);                    object invokee = (noncontrol == null) ? control : noncontrol;                    if (pi != null)                        if (pi.CanWrite)                            pi.SetValue(invokee, value, null);                        else                            throw new FieldAccessException(                                string.Format(                                "{0}.{1} is a read-only property.",                                invokee.GetType().ToString(),                                propertyName                                ));                }            else                throw new ArgumentNullException("Invalid argument.");        }        #endregion    }}

 

InvokeHelper:多線程修改主介面控制項屬性並調用其中方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.