Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Extensions/ControlExntensions.cs

Last change on this file was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

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          // ReSharper disable once AssignNullToNotNullAttribute
20          !control.GetType().IsSubclassOf(propertyInfo.ReflectedType) ||
21          control.GetType().GetProperty(
22              propertyInfo.Name,
23              propertyInfo.PropertyType) == null) {
24        throw new ArgumentException("The lambda expression 'property' must reference a valid property on this Control.");
25      }
26
27      if (control.InvokeRequired) {
28        control.Invoke(new SetPropertyThreadSafeDelegate<TResult>
29        (SetPropertyThreadSafe), control, property, value);
30      } else {
31        control.GetType().InvokeMember(
32            propertyInfo.Name,
33            BindingFlags.SetProperty,
34            null,
35            control,
36            new object[] { value });
37      }
38    }
39  }
40}
Note: See TracBrowser for help on using the repository browser.