首页 技术 正文
技术 2022年11月15日
0 收藏 962 点赞 2,447 浏览 2939 个字
    public class InvokeHelper
{
#region delegates
private delegate object MethodInvoker(Control control, string methodName, params object[] args);
private delegate object PropertyGetInvoker(Control control, object noncontrol, string propertyName);
private delegate void PropertySetInvoker(Control control, object noncontrol, string propertyName, object value);
#endregion #region static methods
// helpers
private static PropertyInfo GetPropertyInfo(Control control, object noncontrol, string propertyName)
{
if (control != null && !string.IsNullOrEmpty(propertyName))
{
PropertyInfo pi = null;
Type t = null; if (noncontrol != null)
t = noncontrol.GetType();
else
t = control.GetType(); pi = t.GetProperty(propertyName); if (pi == null)
throw new InvalidOperationException(
string.Format(
"Can't find property {0} in {1}.",
propertyName,
t.ToString()
)); return pi;
}
else
throw new ArgumentNullException("Invalid argument.");
}
// outlines
public static object Invoke(Control control, string methodName, params object[] args)
{ if (control != null && !string.IsNullOrEmpty(methodName))
if (!control.CheckAccess())
return control.Dispatcher.Invoke(
new MethodInvoker(Invoke),
control,
methodName,
args
);
else
{
MethodInfo mi = null; if (args != null && args.Length > )
{
Type[] types = new Type[args.Length];
for (int i = ; i < args.Length; i++)
{
if (args[i] != null)
types[i] = args[i].GetType();
} mi = control.GetType().GetMethod(methodName, types);
}
else
mi = control.GetType().GetMethod(methodName); // check method info you get
if (mi != null)
return mi.Invoke(control, args);
else
throw new InvalidOperationException("Invalid method.");
}
else
throw new ArgumentNullException("Invalid argument.");
} public static object Get(Control control, string propertyName)
{
return Get(control, null, propertyName);
}
public static object Get(Control control, object noncontrol, string propertyName)
{
if (control != null && !string.IsNullOrEmpty(propertyName))
if (!control.CheckAccess())
return control.Dispatcher.Invoke(new PropertyGetInvoker(Get),
control,
noncontrol,
propertyName
);
else
{
PropertyInfo pi = GetPropertyInfo(control, noncontrol, propertyName);
object invokee = (noncontrol == null) ? control : noncontrol; if (pi != null)
if (pi.CanRead)
return pi.GetValue(invokee, null);
else
throw new FieldAccessException(
string.Format(
"{0}.{1} is a write-only property.",
invokee.GetType().ToString(),
propertyName
)); return null;
}
else
throw new ArgumentNullException("Invalid argument.");
} public static void Set(Control control, string propertyName, object value)
{
Set(control, null, propertyName, value);
}
public static void Set(Control control, object noncontrol, string propertyName, object value)
{
if (control != null && !string.IsNullOrEmpty(propertyName))
if (!control.CheckAccess())
control.Dispatcher.Invoke(new PropertySetInvoker(Set),
control,
noncontrol,
propertyName,
value
);
else
{
PropertyInfo pi = GetPropertyInfo(control, noncontrol, propertyName);
object invokee = (noncontrol == null) ? control : noncontrol; if (pi != null)
if (pi.CanWrite)
pi.SetValue(invokee, value, null);
else
throw new FieldAccessException(
string.Format(
"{0}.{1} is a read-only property.",
invokee.GetType().ToString(),
propertyName
));
}
else
throw new ArgumentNullException("Invalid argument.");
}
#endregion
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,905
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727