Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Extensions/ControlExntensions.cs @ 15287

Last change on this file since 15287 was 14834, checked in by pkimmesw, 8 years ago

#2665 LexicaseSelector, Performance improvements, UI Fixes, Debugger only shows used stacks, fixed Debugger stepping, Added vector expressions, ERCOptions,

File size: 1.4 KB
Line 
1using System;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Extensions {
4  using System.Linq.Expressions;
5  using System.Reflection;
6  using System.Windows.Forms;
7
8  public static class ControlExntensions {
9    private delegate void SetPropertyThreadSafeDelegate<TResult>(Control control, Expression<Func<TResult>> property, TResult value);
10
11    public static void SetPropertyThreadSafe<TResult>(
12        this Control control,
13        Expression<Func<TResult>> property,
14        TResult value) {
15      var propertyInfo = (property.Body as MemberExpression).Member
16          as PropertyInfo;
17
18      if (propertyInfo == null ||
19          !control.GetType().IsSubclassOf(propertyInfo.ReflectedType) ||
20          control.GetType().GetProperty(
21              propertyInfo.Name,
22              propertyInfo.PropertyType) == null) {
23        throw new ArgumentException("The lambda expression 'property' must reference a valid property on this Control.");
24      }
25
26      if (control.InvokeRequired) {
27        control.Invoke(new SetPropertyThreadSafeDelegate<TResult>
28        (SetPropertyThreadSafe), control, property, value);
29      } else {
30        control.GetType().InvokeMember(
31            propertyInfo.Name,
32            BindingFlags.SetProperty,
33            null,
34            control,
35            new object[] { value });
36      }
37    }
38  }
39}
Note: See TracBrowser for help on using the repository browser.