反射找Controller中的方法
/// <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中的方法的更多相关文章
- Spring的Aspect切面类不能拦截Controller中的方法
根本原因在于<aop:aspectj-autoproxy />这句话是在spring的配置文件内,还是在springmvc的配置文件内.如果是在spring的配置文件内,则@Control ...
- ASP.NET MVC4在View中调用当前Controller中的方法
调用当前Controller中的方法 @{ ((HomeController)ViewContext.Controller).Method1(); } 调用静态方法 @{ SomeClass.Meth ...
- spingAOP在springMVC中的使用(我用在拦截controller中的方法。主要用于登录控制)
首先截取了网上的一张配置execution的图片 我在项目中关于aop的配置:如果拦截controller的方法,需要在spring-mvc.xml文件中加入(如果在spring.xml中加入则无法拦 ...
- Spring AOP无法拦截Controller中的方法
想使用AOP Annotation配置Spring MVC的Controller进行拦截, 发现无法拦截Controller的方法, 却可以拦截Service层的方法. 一开始: Spring的配置文 ...
- angularjs 外部调用controller中的方法
angular.element(document.querySelector('[ng-controller=mainCtrl]')).scope().viewGo('tab.VIPPay_Succe ...
- spring MVC controller中的方法跳转到另外controller中的某个method的方法
1. 需求背景 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 本来以为挺简单的一 ...
- Java_通过反射调用类中的方法
先上一个基本的封装: /** * 获取classType * * @param type * @param provinceCode * @param cityCode * @return * @th ...
- C#通过反射获取类中的方法和参数个数,反射调用方法带参数
using System; using System.Reflection; namespace ConsoleApp2 { class Program { static void Main(stri ...
- SpringMVC controller中业务方法的参数、返回值
业务方法的参数 业务方法的参数类型.参数个数是任意的,根据需要使用. 常见的参数类型: HttpServletRequest.HttpServletResponse.HttpSession 获取 ...
随机推荐
- include和require区别
1. include()执行的时候需要引用的文件每次都要进行读取评估; require()执行时需要引用的文件只处理一次(实际上执行时需要引用的文件内容替换了require()语句) 可以看出若有包 ...
- Miller Rabbin素数测试
步骤 ①先写快速幂取模函数 ②MR算法开始 (1)传入两个参数一个是底数一个是n也就是幂数,如果n是一个合数那么可以判定,这个数一定不是素数 (2)然后开始寻找一个奇数的n去计算,如果最后满足a^d% ...
- win10 Ubuntu子系统安装&odoo10社区版安装
参考文档: http://www.cnblogs.com/odoouse/p/5995603.html https://www.jianshu.com/p/58090215bda8 一.win10 U ...
- 学习记录--让我打开另一种思路的SQL
1.显示文章.提交人和最后回复时间 select a.title,a.username,b.adddate from table a, (select max(adddate) adddate fro ...
- Codeforces Round #413(Div. 1 + Div. 2, combined)——ABCD
题目在这里 A.Carrot Cakes 乱七八糟算出两个时间比较一下就行了 又臭又长仅供参考 #include <bits/stdc++.h> #define rep(i, j, k) ...
- SCU Travel
Travel The country frog lives in has n towns which are conveniently numbered by 1,2,…,n . Among n(n− ...
- 某种密码(password.*)
关于某种密码有如下描述:某种密码的原文A是由N个数字组成,而密文B是一个长度为N的01数串,原文和密文的关联在于一个钥匙码KEY.若KEY=∑▒[Ai*Bi],则密文就是原文的一组合法密码.现在有原文 ...
- springCloud学习-服务消费者(rest+ribbon)
1.ribbon简介 spring cloud的Netflix中提供了两个组件实现软负载均衡调用:ribbon和feign. Ribbon 是一个基于 HTTP 和 TCP 客户端的负载均衡器 它可以 ...
- NFS 文件系统的搭建
教程: 一.配置linux内核: 进入打过at91补丁的linux2.6内核源代码树的根目录 make menuconfig进入内核配置程序. 1. Networking ---> Netwo ...
- [转]十五天精通WCF——第一天 三种Binding让你KO80%的业务
转眼wcf技术已经出现很多年了,也在.net界混的风生水起,同时.net也是一个高度封装的框架,作为在wcf食物链最顶端的我们所能做的任务已经简单的不能再简单了, 再简单的话马路上的大妈也能写wcf了 ...