/// <summary>
/// 根据接口编码APICode,调用相应的webapi方法,注意返回值是json字符串
/// </summary>
/// <param name="apiCode">接口编码</param>
/// <param name="requestJson">请求的json字符串</param>
/// <returns>返回的json字符串</returns>
public string Send(string apiCode, string requestJson)
{ var isExsitMethod = false;
var resultJson = string.Empty;
Assembly assembly = Assembly.GetExecutingAssembly(); //加载当前应用程序集
Type[] typearr = assembly.GetTypes().Where(x => x.BaseType != null && x.BaseType.Name == "Controller").ToArray();
//获取类型过滤掉非Controller控制器
MethodInfo methodinfo = null;
Type curtype = null;
foreach (Type type in typearr) //针对每个类型获取详细信息
{
methodinfo = type.GetMethod(apiCode); //获取方法信息
if (methodinfo != null)
{
isExsitMethod = true;
curtype = type;
break;
}
}
if (isExsitMethod)
{
ParameterInfo[] paramsInfo = methodinfo.GetParameters(); //得到指定方法的参数列表
Type tType = paramsInfo[].ParameterType;
var obj = new object[];
if (tType.IsClass) //如果是类,将它的json字符串转换成对象
{
obj[] = Newtonsoft.Json.JsonConvert.DeserializeObject(requestJson, tType); }
object objtype = Assembly.GetExecutingAssembly().CreateInstance(curtype.FullName.ToString(), true, BindingFlags.Default, null, new object[], null, null);
var response = methodinfo.Invoke(objtype, obj);
resultJson = SerializeHelper.ToJson(response);
return resultJson;
} return string.Format("未找到方法{0}", apiCode); }

反射找Controller中的方法的更多相关文章

  1. Spring的Aspect切面类不能拦截Controller中的方法

    根本原因在于<aop:aspectj-autoproxy />这句话是在spring的配置文件内,还是在springmvc的配置文件内.如果是在spring的配置文件内,则@Control ...

  2. ASP.NET MVC4在View中调用当前Controller中的方法

    调用当前Controller中的方法 @{ ((HomeController)ViewContext.Controller).Method1(); } 调用静态方法 @{ SomeClass.Meth ...

  3. spingAOP在springMVC中的使用(我用在拦截controller中的方法。主要用于登录控制)

    首先截取了网上的一张配置execution的图片 我在项目中关于aop的配置:如果拦截controller的方法,需要在spring-mvc.xml文件中加入(如果在spring.xml中加入则无法拦 ...

  4. Spring AOP无法拦截Controller中的方法

    想使用AOP Annotation配置Spring MVC的Controller进行拦截, 发现无法拦截Controller的方法, 却可以拦截Service层的方法. 一开始: Spring的配置文 ...

  5. angularjs 外部调用controller中的方法

    angular.element(document.querySelector('[ng-controller=mainCtrl]')).scope().viewGo('tab.VIPPay_Succe ...

  6. spring MVC controller中的方法跳转到另外controller中的某个method的方法

    1. 需求背景     需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 本来以为挺简单的一 ...

  7. Java_通过反射调用类中的方法

    先上一个基本的封装: /** * 获取classType * * @param type * @param provinceCode * @param cityCode * @return * @th ...

  8. C#通过反射获取类中的方法和参数个数,反射调用方法带参数

    using System; using System.Reflection; namespace ConsoleApp2 { class Program { static void Main(stri ...

  9. SpringMVC controller中业务方法的参数、返回值

    业务方法的参数 业务方法的参数类型.参数个数是任意的,根据需要使用. 常见的参数类型: HttpServletRequest.HttpServletResponse.HttpSession    获取 ...

随机推荐

  1. WING IDE 快捷键

    工欲善其事必先利其器,所以我们无论使用什么编译器,都要熟悉一些快捷键. Ctrl+N新建文件 Ctrl+O 打开文件夹 Ctrl+W 关闭当前文件 Ctrl+S 保存文件 Ctrl+shif+S 另存 ...

  2. windows和linux下 Python2,Python3 的环境及安装

    目录 windows和linux下 Python2,Python3 的环境及安装 window下安装 一. 手动安装 二. pip安装 linux下 安装 更新Python 笔者有话 windows和 ...

  3. 7-15 QQ帐户的申请与登陆

    7-15 QQ帐户的申请与登陆(25 分) 实现QQ新帐户申请和老帐户登陆的简化版功能.最大挑战是:据说现在的QQ号码已经有10位数了. 输入格式: 输入首先给出一个正整数N(≤10​5​​),随后给 ...

  4. 4 pandas模块,Series类

      对gtx图像进行操作,使用numpy知识 如果让gtx这张图片在竖直方向上进行颠倒.   如果让gtx这张图片左右颠倒呢?   如果水平和竖直方向都要颠倒呢?   如果需要将gtx的颜色改变一下呢 ...

  5. 微信小程序如何引用iconfont图标

    最近在研究微信小程序,自己写demo的时候想要引用巴里巴巴图标库的图标,于是: @font-face { font-family: 'iconfont'; src: url('iconfont.eot ...

  6. zabbix 配置——bak

    1.host 配置 create host Parameter Description Host name 主机名,只允许数字,空格,句号,下划线,非主流符号它不支持.zabbix客户端配置文件中的h ...

  7. 1、ceph-deploy之部署ceph集群

    环境说明 server:3台虚拟机,挂载卷/dev/vdb 10G 系统:centos7.2 ceph版本:luminous repo: 公网-http://download.ceph.com,htt ...

  8. nyoj_85_有趣的数_201312122130

    有趣的数 时间限制:3000 ms  |           内存限制:65535 KB 难度:2   描述 把分数按下面的办法排成一个数表. 1/1 1/2 1/3 1/4..... 2/1 2/2 ...

  9. [bzoj3872][Poi2014]Ant colony_树形dp

    Ant colony bzoj-3872 Poi-2014 题目大意:说不明白.....题目链接 注释:略. 想法:两个思路都行. 反正我们就是要求出每个叶子节点到根节点的每个路径权值积. 可以将边做 ...

  10. myeclipse git 上下箭头表示什么

    myeclipse git 上下箭头表示什么? 向上箭头表示本地有提交(commit),但是还没有推(push)到远程代码库中,旁边的数字表示本地commit的次数: 向下箭头表示你拉(fetch)下 ...