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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
using System.Security.Permissions;
[assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name = "FullTrust")]
namespace Cyrons.Forms
{
using System.Reflection;
using System.Windows.Forms;
/// <summary>
/// Bietet Funktionen für den threadsicheren Zugriff auf Objekte von Windows Forms Controls.
/// </summary>
public class ControlCommunicationsManager
{
#region Delegates
private delegate void MethodCaller<ControlType>(
ControlType control,
string methodName,
params object[] parameters)
where ControlType : Control;
private delegate object PropertyValueReaderCallback<ControlType>(
ControlType control, string propertyName)
where ControlType : Control;
private delegate void PropertyValueWriterCallback<ControlType, PropertyType>(
ControlType control, string propertyName, PropertyType value)
where ControlType : Control;
#endregion Delegates
#region Methods
/// <summary>
/// Invokes a control method call.
/// </summary>
/// <typeparam name="ControlType">The type of the control.</typeparam>
/// <param name="control">The control.</param>
/// <param name="methodName">Name of the method.</param>
/// <param name="parameters">Parameters array for the method to be called.</param>
public void InvokeControlMethodCall<ControlType>(
ControlType control,
string methodName, params object[] parameters)
where ControlType : Control
{
if(control.InvokeRequired)
{
MethodCaller<ControlType> callerDelegate = InvokeControlMethodCall;
control.Invoke(callerDelegate, new object[] { control, methodName, parameters });
}
else
{
MethodInfo method = control.GetType().GetMethod(methodName);
method.Invoke(control, parameters);
}
}
/// <summary>
/// Invokes reading of a control property.
/// </summary>
/// <typeparam name="ControlType">The type of the control.</typeparam>
/// <param name="control">The control.</param>
/// <param name="propertyName">Name of the property.</param>
/// <returns></returns>
public object InvokeControlPropertyReader<ControlType>(
ControlType control,
string propertyName)
where ControlType : Control
{
if(control.InvokeRequired)
{
PropertyValueReaderCallback<ControlType> cb = InvokeControlPropertyReader;
return control.Invoke(cb, new object[] { control, propertyName });
}
PropertyInfo property = control.GetType().GetProperty(propertyName);
return property.GetValue(control, null);
}
/// <summary>
/// Invokes writing to a control property.
/// </summary>
/// <typeparam name="ControlType">The type of the control.</typeparam>
/// <typeparam name="PropertyType">The type of the property.</typeparam>
/// <param name="control">The control.</param>
/// <param name="propertyName">Name of the property.</param>
/// <param name="value">The value.</param>
public void InvokeControlPropertyWriter<ControlType, PropertyType>(
ControlType control,
string propertyName,
PropertyType value)
where ControlType : Control
{
if(control.InvokeRequired)
{
PropertyValueWriterCallback<ControlType, PropertyType> cb = InvokeControlPropertyWriter;
control.Invoke(cb, new object[] { control, propertyName, value });
}
else
{
PropertyInfo property = control.GetType().GetProperty(propertyName);
property.SetValue(control, value, null);
}
}
#endregion
}
}
|