Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Extensions/ControlExntensions.cs @ 16752

Last change on this file since 16752 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 1.4 KB
Line 
1using System;
2
3namespace HeuristicLab.Problems.ProgramSynthesis {
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.