博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 通过反射获取MVC Controller里的类名,方法名,参数列表,返回值类型,Description描述,自定义Attribute...
阅读量:6966 次
发布时间:2019-06-27

本文共 3399 字,大约阅读时间需要 11 分钟。

需要反射的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
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 > 0) richTextBox1.AppendText((attrs[0] 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"); } } }

转载地址:http://bnfsl.baihongyu.com/

你可能感兴趣的文章
GENERIC FRAMEWORK MODEL OF JAVA PLATFORM
查看>>
我的友情链接
查看>>
linux
查看>>
Linux 下 hosts 应用
查看>>
mysql单机多实例——方法1
查看>>
我的友情链接
查看>>
Github上AI在银行和保险的应用列表
查看>>
数据绑定设计器的使用
查看>>
[转载] 大道至简:软件工程实践者的思想——第四章 流于形式的沟通
查看>>
Spring Batch_ItemReaders and ItemWriters
查看>>
会话中的存储技术和一些细节
查看>>
SQL 基礎語句-case
查看>>
php 面向对象 创建OOP
查看>>
项目发布的时候报错
查看>>
iptables 定义规则
查看>>
如何使用 APM 搞定 PHP 应用的性能优化?
查看>>
三大框架开发时,spring配置文件出现异常
查看>>
java命令查看jvm内存
查看>>
Spread for Windows Forms高级主题(7)---自定义打印的外观
查看>>
win7加入域的脚本
查看>>