| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- 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 MeterModelLibrary
- {
- public class ExecuteMethod
- {
- /// <summary>
- /// 调用并执行指定类里面的函数
- /// </summary>
- /// <param name="nameSpace">命名空间</param>
- /// <param name="className">需要调用的类名(包含其命名空间)</param>
- /// <param name="methodName">需要调用的方法名</param>
- /// <param name="parameters">传递的参数值</param>
- /// <return>返回值</return>
- public RESTfulResult<T> GetAndExecuteMethod<T>(string nameSpace, string className, string methodName, object[] parameters = null)
- {
- RESTfulResult<T> rm = new RESTfulResult<T>();
- 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;
- }
- }
- }
|