需要反射的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的更多相关文章

  1. c# 获取方法所在的命名空间 类名 方法名

    平时我们在记录日志的时候难免会需要直接记录当前方法的路径,以便查找,但是每次都输入方法名称非常的繁琐,同时如果修改了方法名称也要去手动修改日志内容,真的是劳命伤财啊,所以有了如下方法则可解决我们的大难 ...

  2. C# 获取方法所在的 命名空间 类名 方法名

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  3. Asp.net MVC 中Controller返回值类型ActionResult

    [Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...

  4. C#反射 获取程序集信息和通过类名创建类实例(转载)

    C#反射获取程序集信息和通过类名创建类实例 . System.Reflection 命名空间:包含通过检查托管代码中程序集.模块.成员.参数和其他实体的元数据来检索其相关信息的类型. Assembly ...

  5. C# 反射总结 获取 命名空间 类名 方法名

    一.获取 命名空间 类名 方法名 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  6. java 反射获取方法返回值类型

    //ProceedingJoinPoint pjp //获取方法返回值类型 Object[] args = pjp.getArgs(); Class<?>[] paramsCls = ne ...

  7. Spring MVC controller返回值类型

    SpringMVC controller返回值类型: 1 String return "user":将请求转发到user.jsp(forword) return "red ...

  8. SpringMVC源码学习:容器初始化+MVC初始化+请求分发处理+参数解析+返回值解析+视图解析

    目录 一.前言 二.初始化 1. 容器初始化 根容器查找的方法 容器创建的方法 加载配置文件信息 2. MVC的初始化 文件上传解析器 区域信息解析器 handler映射信息解析 3. Handler ...

  9. Spring框架下的 “接口调用、MVC请求” 调用参数、返回值、耗时信息输出

    主要拦截前端或后天的请求,打印请求方法参数.返回值.耗时.异常的日志.方便开发调试,能很快定位到问题出现在哪个方法中. 前端请求拦截,mvc的拦截器 import java.util.Date; im ...

随机推荐

  1. windows系统如何通过Xshell 客户端连接 linux系统(主要介绍ubuntu系统)

    一. 1.查看ubuntu系统的ip地址:ifconfig 在window系统运行窗口下:ping ubuntu系统的IP地址:例如:ping 192.168.163.129 出现下述命令就是ping ...

  2. databus编译: Execution failed for task ':databus-core:databus-core-impl:compileJava'.

    在编译databus的过程中,出现了无法找到jdk的错误: 在/etc/.bashrc和/etc/profile中都配置了JAVA_HOME,依然报错,重启后还是报错,原因的是ubuntu中默认的jd ...

  3. update document in mongodb using java -摘自网络

    update document in mongodb using java: Mongodb driver provides functionality to update document in m ...

  4. mongoimport mongo导入Json的用法

    //导入json mongoimport --db ML_OER --collection lecture --file /home/tmp/course_temp.json //导入Json数组 m ...

  5. 好的 IOS 学习网站

    http://www.objc.io/contributors.html codeproject. http://www.codeproject.com/KB/iPhone/

  6. Push UIViewController with different orientation to previous

    转自:http://stackoverflow.com/questions/6695837/push-uiviewcontroller-with-different-orientation-to-pr ...

  7. centos 7 五笔安装

    # yum install ibus ibus-table-wubi 需要重启动

  8. 如何理解java泛型类

    //泛型代码 public class Pair<T>{ private T first=null; private T second=null; public Pair(T fir,T  ...

  9. 【教程】Source Insight 关联 .S文件,汇编文件

    加载.s汇编文件 做ARM嵌入式开发时,有时得整汇编代码,但在SI里建立PROJECT并ADD TREE的时候,根据默认设置并不会把该TREE里面所有汇编文件都包含进来,默认只加了.inc和.asm后 ...

  10. 使用SpringBoot的yml文件配置时踩的一个坑

    问题描述:使用SpringBoot整合redis进行yml配置的时候,启动工程报错,提示加载application.yml配置文件失败: ::27.430 [main] ERROR org.sprin ...