Winform常用方法

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;

namespace EbookToBBs
{
    public class Tool
    {
        public static Type RtbType = typeof(RichTextBox);

        /// <summary>
        /// 停止一个线程,如果没有停止则方法不返回
        /// </summary>
        /// <param name="thread"></param>
        public static void StopThread(Thread thread)
        {
            if (thread != null && thread.ThreadState != ThreadState.Stopped)
            {
                thread.Abort();
                while (true)
                {                 
                    if (thread.ThreadState == ThreadState.Stopped)
                    {                       
                        return;
                    }                  
                    Thread.Sleep(10);
                }
            }
        }

        /// <summary>
        /// 为RichTextBox添加内容
        /// </summary>
        /// <param name="rtb"></param>
        /// <param name="text"></param>
        public static void AppendText(RichTextBox rtb, string text)
        {
            //如果不是在UI线程上操作rtb
            if (rtb.InvokeRequired)
            {
                rtb.Invoke(new Action<string>(delegate(string txt)
                {
                    rtb.AppendText(txt);
                    rtb.AppendText("\r\n");
                }), text);
            }
            else
            {
                rtb.AppendText(text);
            }
        }

        /// <summary>
        /// 使用反射调用RichTextBox的方法
        /// </summary>
        /// <param name="rtb"></param>
        /// <param name="methodName"></param>
        public static void Method(RichTextBox rtb, string methodName)
        {
            MethodInfo method = GetMethod(RtbType, methodName);
            Invoke(rtb, method, null);
        }

        /// <summary>
        /// 使用反射调用RichTextBox的方法
        /// </summary>
        /// <param name="rtb"></param>
        /// <param name="methodName"></param>
        public static void Method(RichTextBox rtb, string methodName, Type[] typeArr, ParameterModifier[] paraModiferArr, params object[] args)
        {
            MethodInfo method = GetMethod(RtbType, methodName, typeArr, paraModiferArr);
            if (method == null)
            {
                return;
            }
            Invoke(rtb, method, args);
        }

        /// <summary>
        /// 调用某个实例的某个方法
        /// </summary>
        /// <param name="rtb">实例</param>
        /// <param name="method">方法</param>
        /// <param name="args">参数</param>
        public static void Invoke(RichTextBox rtb, MethodInfo method, params object[] args)
        {
            //如果不是在UI线程上操作rtb
            if (rtb.InvokeRequired)
            {
                rtb.Invoke(new Action<object[]>(delegate(object[] objArr)
                    {
                        method.Invoke(rtb, objArr);
                    }), new object[] { args });
            }
            else
            {
                method.Invoke(rtb, args);
            }
        }

        #region 获取一个类型的方法
        /// <summary>
        /// 获取一个没有参数的方法
        /// </summary>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static MethodInfo GetMethod(Type type, string name)
        {
            MethodInfo method = type.GetMethod(name, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
            return method;
        }

        /// <summary>
        /// 获取一个有参数的方法
        /// </summary>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <param name="typeArr"></param>
        /// <param name="paraModiferArr"></param>
        /// <returns></returns>
        public static MethodInfo GetMethod(Type type, string name, Type[] typeArr, ParameterModifier[] paraModiferArr)
        {
            MethodInfo method = type.GetMethod(name, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance, null, typeArr, paraModiferArr);
            return method;
        }
        #endregion
    }
}
posted @ 2011-05-04 18:21  再快一点  阅读(427)  评论(0编辑  收藏  举报