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 ...
随机推荐
- Java 8 forEach examples遍历例子
1. forEach and Map 1.1 Normal way to loop a Map. Map<String, Integer> items = new HashMap<& ...
- Best practices for Express app structure
Node和Express没有一个相对严格的文件或者是文件夹结构,因此你可以按照自己的想法来构建你的web项目,特别是对一些小的项目来说,他很容易学习. 然而当你的项目变得越来越大,所面临的情况越来越复 ...
- [转]cubemap soft shadow
https://community.arm.com/graphics/b/blog/posts/dynamic-soft-shadows-based-on-local-cubemap
- 微信小程序调用蓝牙功能控制车位锁
第一次学用微信小程序,项目需要,被逼着研究了一下,功能是调用微信小程序的蓝牙功能,连接上智能车位锁,控制升降,大概步骤及调用的小程序接口API如下: 1.打开蓝牙模块 wx.openBluetooth ...
- Atitit 图像处理之编程之类库调用的接口api cli gui ws rest attilax大总结.docx
Atitit 图像处理之编程之类库调用的接口api cli gui ws rest attilax大总结.docx 1. 为什么需要接口调用??1 1.1. 为了方便集成复用模块类库1 1.2. 嫁 ...
- 令新手头疼的modelsim库编译
估计很多人买了CB哥的书来看吧,他们在学习modelsim仿真的过程中可能遇到过明明是按照书上的步骤添加器件库的了,但还是出现如下的错误: 首先,我想说的是CB哥书上的modelsim-altera1 ...
- android 自己定义视频播放器之2/1
非常久没更新博客,相信大家年后都比較忙. 今天给大家带来了一款视频播放器,首先确认的得有几点. 1.首先得有个播放视频的view. 2.加点额外功能进去左边上下滑动调节亮度,右边上下滑动调节声量: 3 ...
- Postman调用WebService,包括头验证部分
Postman调用WebService时,Body中选择Raw,最右端选择XML(txt/xml),然后把某个方法显示在页面的xml拷贝到请求框中即可,如下图: 以下是postman中的设置,
- npm WARN build `npm build` called with no arguments. Did you mean to `npm run-script build`?
跑npm build结果如下: npm WARN build `npm build` called with no arguments. Did you mean to `npm run-script ...
- Android调用Webservice发送文件
一服务器端C#这里有三个上传方法1.uploadFile( byte []bs, String fileName); PC机操作是没有问题2. uploadImage(String filename, ...