zoukankan      html  css  js  c++  java
  • WPF非UI线程获取修改控件属性值的方法

        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.CheckAccess())
                        return control.Dispatcher.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.CheckAccess())
                        return control.Dispatcher.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.CheckAccess())
                        control.Dispatcher.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
        }
  • 相关阅读:
    html5+css3中的background: -moz-linear-gradient 用法 (转载)
    CentOS 安装Apache服务
    Linux 笔记
    CURL 笔记
    Spring Application Context文件没有提示功能解决方法
    LeetCode 389. Find the Difference
    LeetCode 104. Maximum Depth of Binary Tree
    LeetCode 520. Detect Capital
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 136. Single Number
  • 原文地址:https://www.cnblogs.com/tangxueyang/p/5736689.html
Copyright © 2011-2022 走看看