using System; namespace HeuristicLab.Problems.ProgramSynthesis.Push.Extensions { using System.Linq.Expressions; using System.Reflection; using System.Windows.Forms; public static class ControlExntensions { private delegate void SetPropertyThreadSafeDelegate(Control control, Expression> property, TResult value); public static void SetPropertyThreadSafe( this Control control, Expression> property, TResult value) { var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo; if (propertyInfo == null || !control.GetType().IsSubclassOf(propertyInfo.ReflectedType) || control.GetType().GetProperty( propertyInfo.Name, propertyInfo.PropertyType) == null) { throw new ArgumentException("The lambda expression 'property' must reference a valid property on this Control."); } if (control.InvokeRequired) { control.Invoke(new SetPropertyThreadSafeDelegate (SetPropertyThreadSafe), control, property, value); } else { control.GetType().InvokeMember( propertyInfo.Name, BindingFlags.SetProperty, null, control, new object[] { value }); } } } }