C# 通过反射获取MVC Controller里的类名,方法名,参数列表,返回值类型,Description描述,自定义Attribute
需要反射的DLL里的一个类:
namespace ElegantWM.WebUI.Areas.Admin.Controllers
{
[Description("功能模块管理")]
public class ModuleController : BaseController
{
[Action]
[Description("根据系统编号,获取系统的功能模块")]
[HttpGet]
public string Get(Guid sysId)
{
...
return json;
} [Action]
[Description("新建功能模块")]
[HttpPost]
public JsonResult Create(WMS_Module module)
{
...
} [Action]
[Description("修改功能模块")]
[HttpPut]
public JsonResult Update(WMS_Module module)
{
...
} [Action]
[Description("删除功能模块")]
[HttpDelete]
public JsonResult Delete(WMS_Module module)
{
...
}
}
}
反射代码:
Assembly assembly = Assembly.LoadFrom(@"ElegantWM.WebUI.dll");
var types = assembly.GetTypes();
foreach (var type in types)
{
if (type.BaseType.Name == "BaseController")//如果是Controller
{
var members = type.GetMethods();
foreach (var member in members)
{
//获取HTTPAttribute
IEnumerable<Attribute> atts = member.GetCustomAttributes();
bool isAction = false;
foreach (Attribute a in atts)
{
if (a.GetType().Name == "Action" || a.GetType().Name == "Page")
{
richTextBox1.AppendText(a.GetType().Name + "\r\n");
isAction = true;
}
else if (a.ToString().Contains("System.Web.Mvc.Http"))
{
richTextBox1.AppendText(a.GetType().Name.Replace("Http", "").Replace("Attribute", "") + "\r\n");
}
}
if (!isAction) continue; //获取返回值
richTextBox1.AppendText(member.ReturnType.Name + "\r\n"); //获取方法说明
object[] attrs = member.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
if (attrs.Length > )
richTextBox1.AppendText((attrs[] as System.ComponentModel.DescriptionAttribute).Description + "\r\n"); //获取参数
ParameterInfo[] param = member.GetParameters();
StringBuilder sb = new StringBuilder();
foreach (ParameterInfo pm in param)
{
sb.Append(pm.ParameterType.Name + " " + pm.Name + ",");
}
richTextBox1.AppendText("(" + sb.ToString().Trim(',') + ")" + "\r\n"); //获取方法名称
richTextBox1.AppendText(member.Name + "\r\n"); //获取类名
richTextBox1.AppendText(member.DeclaringType.Name + "\r\n"); richTextBox1.AppendText("--------------------------\r\n"); }
}
}
C# 通过反射获取MVC Controller里的类名,方法名,参数列表,返回值类型,Description描述,自定义Attribute的更多相关文章
- c# 获取方法所在的命名空间 类名 方法名
平时我们在记录日志的时候难免会需要直接记录当前方法的路径,以便查找,但是每次都输入方法名称非常的繁琐,同时如果修改了方法名称也要去手动修改日志内容,真的是劳命伤财啊,所以有了如下方法则可解决我们的大难 ...
- C# 获取方法所在的 命名空间 类名 方法名
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
- C#反射 获取程序集信息和通过类名创建类实例(转载)
C#反射获取程序集信息和通过类名创建类实例 . System.Reflection 命名空间:包含通过检查托管代码中程序集.模块.成员.参数和其他实体的元数据来检索其相关信息的类型. Assembly ...
- C# 反射总结 获取 命名空间 类名 方法名
一.获取 命名空间 类名 方法名 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- java 反射获取方法返回值类型
//ProceedingJoinPoint pjp //获取方法返回值类型 Object[] args = pjp.getArgs(); Class<?>[] paramsCls = ne ...
- Spring MVC controller返回值类型
SpringMVC controller返回值类型: 1 String return "user":将请求转发到user.jsp(forword) return "red ...
- SpringMVC源码学习:容器初始化+MVC初始化+请求分发处理+参数解析+返回值解析+视图解析
目录 一.前言 二.初始化 1. 容器初始化 根容器查找的方法 容器创建的方法 加载配置文件信息 2. MVC的初始化 文件上传解析器 区域信息解析器 handler映射信息解析 3. Handler ...
- Spring框架下的 “接口调用、MVC请求” 调用参数、返回值、耗时信息输出
主要拦截前端或后天的请求,打印请求方法参数.返回值.耗时.异常的日志.方便开发调试,能很快定位到问题出现在哪个方法中. 前端请求拦截,mvc的拦截器 import java.util.Date; im ...
随机推荐
- 设置Myeclipse中的代码格式化、注释模板及保存时自动格式化
1:设置注释的模板: 下载此模板: codetemplates.xml 搜索Dangzhang,将其改为你自己的姓名,保存 打开eclipse/myeclipse选择 window-->Pre ...
- 增加nginx虚拟主机配置文件(conf.d)
有时候我们按照了nginx后发现配置文件只有一个,/etc/nginx/nginx.conf 所有的配置包括虚拟目录也在此文件中配置, 这样当虚拟主机多了管理就有些不方便了, 这是需要我们把配置文件拆 ...
- struts2(三) 输入校验和拦截器
前面知道了struts2的架构图和struts2的自动封装表单参数和数据类型自动转换,今天来学struts2的第三第四个东西,输入校验和拦截器, --WH 一.输入校验 在以前我们写一个登录页面时,并 ...
- 深入理解Linux内核-进程调度
1.什么时候进行进程切换 调度策略目标:1.进程响应尽量快:2.后台作业吞吐量尽量高:3.尽可能避免进程饥饿:4.低优先级和高优先级进程需要尽量调和. 调度策略:决定什么时候选择什么进程运行的规则.基 ...
- sql server FCI and always on
https://docs.microsoft.com/en-us/sql/sql-server/failover-clusters/high-availability-solutions-sql-se ...
- Multi-Cloud & Kubernetes: Cloud Academy November 2018 Data Report
https://cloudacademy.com/research/multi-cloud-kubernetes-devops-cloud-academy-data-report-nov-18/ No ...
- Chrome浏览器在Windows 和 Linux下的键盘快捷方式
Windows 键盘快捷键 标签页和窗口快捷键 Ctrl+N 打开新窗口. Ctrl+T 打开新标签页. Ctrl+Shift+N 在隐身模式下打开新窗口. 按 Ctrl+O,然后选择文件. 通过 G ...
- 每日英语:Pediatricians Set Limits on Screen Time
Parents should ban electronic media during mealtimes and after bedtime as part of a comprehensive 'f ...
- cxf、struts、spring中web.xml过滤url问题解决方案
利用struts2自带的正则匹配,应该说这算是最官方的解决方案了 在struts.properties中加正则匹配 struts.action.excludePattern=/webservice/. ...
- ImportError: cannot import name 'main'的解决办法
一.现象 使用pip出现如下提示: Traceback (most recent call last): File "/usr/bin/pip3", line 9, in < ...