1、MethodInfo类是在System.Reflection命名空间底下,既然是在Reflection空间底下。故名思议关于反射相关的操作,其中比较重要的方法是Invoke()方法,它
是加载相同程序集的方法。简单用法
string command = "AnnouncementSave";
//通过反射获取调用的具体方法
System.Reflection.MethodInfo method = this.GetType().GetMethod(command);
if (method != null)
{
//Execute Method
method.Invoke(this, new object[] { context });
}
调用方法:
public void AnnouncementSave(HttpContext context)
{
}
2.一段使用了很多反射技术的代码
public CommonValid(object m){if (m != null){PropertyInfo[] pis = m.GetType().GetProperties();if (pis != null){foreach (PropertyInfo pi in pis){object[] attrs = System.Attribute.GetCustomAttributes(pi);foreach (object attr in attrs){if (attr.GetType().Namespace == "Soffice.DevelopFramework.Common.Valid"){Type t = typeof(Soffice.DevelopFramework.Common.Valid.Methods);MethodInfo mi = t.GetMethod(attr.GetType().Name);if (mi != null){bool result = (bool)mi.Invoke(null, new object[] { attr, pi.GetValue(m, null) });if (!result){ValidResult validResult = new ValidResult();MethodInfo MethodGetNum = attr.GetType().GetMethod("GetNum");if (MethodGetNum != null){validResult.Num = (int)MethodGetNum.Invoke(attr, null);}//ValidResult validResult = new ValidResult();//validResult.Num = int.Parse(t.GetProperty("Num").GetValue(attr, null).ToString());//listValid.Add(validResult); }}}}}}}}