using Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using com.hnshituo.core.webapp.vo; namespace MeterSceneLibrary { public class ExecuteMethod { /// /// 调用并执行指定类里面的函数 /// /// 命名空间 /// 需要调用的类名(包含其命名空间) /// 需要调用的方法名 /// 传递的参数值 /// 返回值 public RESTfulResult GetAndExecuteMethod(string nameSpace, string className, string methodName, object[] parameters = null) { RESTfulResult rm = new RESTfulResult(); string strCls = nameSpace + "." + className; try { var type = Type.GetType(strCls); if (type == null) { rm.Succeed = false; rm.ResultMessage = "类[" + strCls + "]不存在"; } var obj = type.Assembly.CreateInstance(strCls); //调用其方法 var method = type.GetMethod(methodName); if (method == null) { rm.Succeed = false; rm.ResultMessage = "类[" + strCls + "]不存在方法[" + methodName + "]"; } //*/ //执行方法 rm.Data = (T)method.Invoke(obj, parameters); rm.Succeed = true; } catch (Exception ex) { rm.Succeed = false; rm.ResultMessage = "类[" + strCls + "]的方法[" + methodName + "]执行失败:" + ex.Message; } return rm; } } public class rtInfo { /// /// 为true则执行成功了,为false则未成功 /// public bool result { get; set; } /// /// 执行后返回的信息 /// public string resultInfo { get; set; } /// /// 是否报错导致的false,如果这里未true则认为是报错了 /// public bool isError { get; set; } /// /// 为1时提示取样 /// public string message { get; set; } } }