forked from jasonpang/RemoteDesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlExtensions.cs
More file actions
38 lines (32 loc) · 1.48 KB
/
Copy pathControlExtensions.cs
File metadata and controls
38 lines (32 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows.Forms;
namespace Model.Extensions
{
/// <remarks>Ian Kemp from http://stackoverflow.com/questions/661561/how-to-update-gui-from-another-thread-in-c </remarks>
public static class ControlExtensions
{
private delegate void SetDelegate<TResult>(Control @this, Expression<Func<TResult>> property, TResult value);
public static void Set<TResult>(this Control @this, Expression<Func<TResult>> property, TResult value)
{
if (property.Body == null)
throw new ArgumentNullException("property.Body");
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 SetDelegate<TResult>(Set), new object[] {@this, property, value});
}
else
{
@this.GetType().InvokeMember(propertyInfo.Name, BindingFlags.SetProperty, null, @this, new object[] {value});
}
}
}
}