【问题标题】:c# Setting / Getting properties of controls to / from another threadc#设置/获取控件的属性到/从另一个线程
【发布时间】:2016-12-07 10:03:15
【问题描述】:

我有此代码用于将控制参数设置到另一个线程:

private delegate void SetPropertySafeDelegate<TResult>(System.Windows.Forms.Control @this, Expression<Func<TResult>> property, TResult value);

    public static void SetProperty<TResult>(this System.Windows.Forms.Control @this, Expression<Func<TResult>> property, TResult value)
    {
        var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;

        if (propertyInfo == null || !@this.GetType().IsSubclassOf(propertyInfo.ReflectedType) || @this.GetType().GetProperty(propertyInfo.Name, propertyInfo.PropertyType) == null)
        {
            throw new ArgumentException("The lambda expression 'property' must reference a valid property on this Control.");
        }

        if (@this.InvokeRequired)
        {
            @this.Invoke(new SetPropertySafeDelegate<TResult>(SetProperty), new object[] { @this, property, value });
        }
        else
        {
            @this.GetType().InvokeMember(propertyInfo.Name, BindingFlags.SetProperty, null, @this, new object[] { value });
        }
    }

它是这样工作的:

 label1.SetProperty(() => label1.Text, "xxx");

但我还需要它来处理其他事情,例如:

checkBox4.SetProperty(() => checkBox4.Checked, true);

这不起作用。

我需要的第二件事是获取控制值的相同功能。

非常感谢您的建议。

【问题讨论】:

    标签: multithreading get set controls


    【解决方案1】:

    适合我的解决方案:

        /// <summary>
        /// Gets control property. Usage: label1.GetProperty2(() => label1.Text);
        /// </summary>
        public static object GetProperty2<TResult>(this Control @this, Expression<Func<TResult>> property)
        {
            var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;
    
            return @this.GetType().GetProperty(propertyInfo.Name, propertyInfo.PropertyType).GetValue(@this, null);
        }
    
        /// <summary>
        /// Sets control property. Usage: label1.SetProperty2(() => label1.Text, "Zadej cestu k modelu.");
        /// </summary>
        public static void SetProperty2<TResult>(this Control @this, Expression<Func<TResult>> property, TResult value)
        {
            var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;
    
            if (@this.InvokeRequired)
            {
                @this.Invoke(new SetPropertySafeDelegate<TResult>(SetProperty2), new object[] { @this, property, value });
            }
            else
            {
                @this.GetType().InvokeMember(propertyInfo.Name, BindingFlags.SetProperty, null, @this, new object[] { value });
            }
        }
        private delegate void SetPropertySafeDelegate<TResult>(Control @this, Expression<Func<TResult>> property, TResult value);
    

    【讨论】:

      猜你喜欢
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多