delegate
void
bindvalue(
object
Instance,
string
Property,
object
value);
delegate
object
invokemethod(
object
Instance,
string
Method,
object
[] parameters);
delegate
object
invokepmethod(
object
Instance,
string
Property,
string
Method,
object
[] parameters);
delegate
object
invokechailmethod(
object
InstanceInvokeRequired,
object
Instance,
string
Method,
object
[] parameters);
///
<summary>
///
委托设置对象属性
///
</summary>
///
<param name="Instance">
对象
</param>
///
<param name="Property">
属性名
</param>
///
<param name="value">
属性值
</param>
private
void
SetValue(
object
Instance,
string
Property,
object
value)
{
if
((
bool
)GetPropertyValue(Instance,
"
InvokeRequired
"
))
{
bindvalue d
=
new
bindvalue(SetValue);
this
.Invoke(d,
new
object
[] { Instance, Property, value });
}
else
{
SetPropertyValue(Instance, Property, value);
}
}
///
<summary>
///
委托执行实例的方法
///
</summary>
///
<param name="Instance">
类实例
</param>
///
<param name="Method">
方法名
</param>
///
<param name="parameters">
参数列表
</param>
///
<returns>
返回值
</returns>
private
object
InvokeMethod(
object
Instance,
string
Method,
object
[] parameters)
{
if
((
bool
)GetPropertyValue(Instance,
"
InvokeRequired
"
))
{
invokemethod d
=
new
invokemethod(InvokeMethod);
return
this
.Invoke(d,
new
object
[] { Instance, Method, parameters });
}
else
{
return
MethodInvoke(Instance, Method, parameters);
}
}
///
<summary>
///
委托执行实例的方法
///
</summary>
///
<param name="InstanceInvokeRequired">
窗体控件对象
</param>
///
<param name="Instance">
需要执行方法的对象
</param>
///
<param name="Method">
方法名
</param>
///
<param name="parameters">
参数列表
</param>
///
<returns>
返回值
</returns>
private
object
InvokeChailMethod(
object
InstanceInvokeRequired,
object
Instance,
string
Method,
object
[] parameters)
{
if
((
bool
)GetPropertyValue(InstanceInvokeRequired,
"
InvokeRequired
"
))
{
invokechailmethod d
=
new
invokechailmethod(InvokeChailMethod);
return
this
.Invoke(d,
new
object
[] { InstanceInvokeRequired, Instance, Method, parameters });
}
else
{
return
MethodInvoke(Instance, Method, parameters);
}
}
///
<summary>
///
委托执行实例的属性的方法
///
</summary>
///
<param name="Instance">
类实例
</param>
///
<param name="Property">
属性名
</param>
///
<param name="Method">
方法名
</param>
///
<param name="parameters">
参数列表
</param>
///
<returns>
返回值
</returns>
private
object
InvokePMethod(
object
Instance,
string
Property,
string
Method,
object
[] parameters)
{
if
((
bool
)GetPropertyValue(Instance,
"
InvokeRequired
"
))
{
invokepmethod d
=
new
invokepmethod(InvokePMethod);
return
this
.Invoke(d,
new
object
[] { Instance, Property, Method, parameters });
}
else
{
return
MethodInvoke(GetPropertyValue(Instance, Property), Method, parameters);
}
}
///
<summary>
///
获取实例的属性值
///
</summary>
///
<param name="ClassInstance">
类实例
</param>
///
<param name="PropertyName">
属性名
</param>
///
<returns>
属性值
</returns>
private
static
object
GetPropertyValue(
object
ClassInstance,
string
PropertyName)
{
Type myType
=
ClassInstance.GetType();
PropertyInfo myPropertyInfo
=
myType.GetProperty(PropertyName, BindingFlags.Instance
|
BindingFlags.NonPublic
|
BindingFlags.Public);
return
myPropertyInfo.GetValue(ClassInstance,
null
);
}
///
<summary>
///
设置实例的属性值
///
</summary>
///
<param name="ClassInstance">
类实例
</param>
///
<param name="PropertyName">
属性名
</param>
private
static
void
SetPropertyValue(
object
ClassInstance,
string
PropertyName,
object
PropertyValue)
{
Type myType
=
ClassInstance.GetType();
PropertyInfo myPropertyInfo
=
myType.GetProperty(PropertyName, BindingFlags.Instance
|
BindingFlags.NonPublic
|
BindingFlags.Public);
myPropertyInfo.SetValue(ClassInstance, PropertyValue,
null
);
}
///
<summary>
///
执行实例的方法
///
</summary>
///
<param name="ClassInstance">
类实例
</param>
///
<param name="MethodName">
方法名
</param>
///
<param name="parameters">
参数列表
</param>
///
<returns>
返回值
</returns>
private
static
object
MethodInvoke(
object
ClassInstance,
string
MethodName,
object
[] parameters)
{
if
(parameters
==
null
)
{
parameters
=
new
object
[
0
];
}
Type myType
=
ClassInstance.GetType();
Type[] types
=
new
Type[parameters.Length];
for
(
int
i
=
0
; i
<
parameters.Length;
++
i)
{
types[i]
=
parameters[i].GetType();
}
MethodInfo myMethodInfo
=
myType.GetMethod(MethodName, types);
return
myMethodInfo.Invoke(ClassInstance, parameters);
}