在C#中,实现动态获取类和方法主要通过反射来实现,要引用System.Reflection。

public ActionResult GetControllerAndAction()
List<Type> controllerTypes = new List<Type>(); //创建控制器类型列表
var assembly = Assembly.Load("MySoft.UI"); //加载程序集
controllerTypes.AddRange(assembly.GetTypes().Where(type => typeof(IController).IsAssignableFrom(type) && type.Name!="ErrorController")); //获取程序集下所有的类,通过Linq筛选继承IController类的所有类型
StringBuilder jsonBuilder = new StringBuilder(); //创建动态字符串,拼接json数据 注:现在json类型传递数据比较流行,比xml简洁
jsonBuilder.Append("[");
foreach (var controller in controllerTypes)//遍历控制器类
{
jsonBuilder.Append("{\"controllerName\":\"");
jsonBuilder.Append(controller.Name);
jsonBuilder.Append("\",\"controllerDesc\":\"");
jsonBuilder.Append((controller.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute)==null?"" : (controller.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute).Description); //获取对控制器的描述Description
jsonBuilder.Append("\",\"action\":[");
var actions = controller.GetMethods().Where(method => method.ReturnType.Name == "ActionResult"); //获取控制器下所有返回类型为ActionResult的方法,对MVC的权限控制只要限制所以的前后台交互请求就行,统一为ActionResult
foreach (var action in actions)
{
jsonBuilder.Append("{\"actionName\":\"");
jsonBuilder.Append(action.Name);
jsonBuilder.Append("\",\"actionDesc\":\"");
jsonBuilder.Append((action.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute) == null ? "" : (action.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute).Description); //获取对Action的描述
jsonBuilder.Append("\"},");
}
jsonBuilder.Remove(jsonBuilder.Length - , );
jsonBuilder.Append("]},");
}
jsonBuilder.Remove(jsonBuilder.Length - , );
jsonBuilder.Append("]");
return Content(jsonBuilder.ToString(),"json/text");t");

C#获取类以及类下的方法(用于Asp.Net MVC)的更多相关文章

  1. Creating Custom Helper Methods 创建自定义辅助器方法----辅助器方法 ------ 精通ASP.NET MVC 5

    创建内联的辅助器方法 和 拓展方法 好像类似的功能. 不过写在前台更直观

  2. 使用整体模型模板辅助器 Using Whole-Model Templated Helpers 模板辅助器方法 精通ASP.NET MVC 5

    怎么会

  3. Creating Form Elements --Using BeginForm and EndForm 使用内建的Form辅助器方法 精通ASP.NET MVC 5

    Using the BeginForm and EndForm Helper Methods in the CreatePerson.cshtml File

  4. 【ASP.NET MVC系列】浅谈ASP.NET MVC八大类扩展(上篇)

    lASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操 ...

  5. 6. ASP.NET MVC 5.0 中的HTML Helper【HTML 帮助类】

    这篇文章,我将带领大家学习HTML Helper.[PS:上一篇-->5.ASP.NET MVC 中的Area[区域]是什么] HTML Helpers是用来创建HTML标签进而创建HTML控件 ...

  6. ASP.NET MVC的帮助类HtmlHelper和UrlHelper

    在ASP.NET MVC框架中没有了自己的控件,页面显示完全就回到了写html代码的年代.还好在asp.net mvc框架中也有自带的HtmlHelper和UrlHelper两个帮助类.另外在MvcC ...

  7. [转]ASP.NET MVC的帮助类HtmlHelper和UrlHelper

    本文转自:http://www.cnblogs.com/greatandforever/archive/2010/04/20/1715914.html?login=1 在ASP.NET MVC框架中没 ...

  8. ASP.NET MVC @html帮助类

    原文:https://www.cnblogs.com/caofangsheng/p/10462494.html HTML Helpers是用来创建HTML标签进而创建HTML控件的.HTML Help ...

  9. day20-双下new方法,单例模式

    # 1. __new__:构造方法,它创造对象,程序员口头语:new一个对象.先执行__new__方法再执行___init__方法. class Goods: def __init__(self):# ...

随机推荐

  1. Atitit org.eclipse.jdt 的ast 架构 Eclipse JDT API spec

    Atitit org.eclipse.jdt 的ast 架构 Eclipse JDT API spec 继承树1 Expression的子类1 获取子类2 继承树 Astnode>express ...

  2. D. Book of Evil

    D. Book of Evil time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  3. 【关于新版Cocos2dx/Cocos2d-JS】安装包和使用方式的变化

    最近有读者反馈说,最新的Cocos2dx或者说2d-JS下载之后跟书本说的安装方式相差很大. 刚下载最新版3.9试了一下,其实用法还是没有变的. 新版把cocos2dx和cocos2d-js合并了,这 ...

  4. sqlserver数据库安全函数、配置函数、游标函数、行级函数、排名函数、元数据函数、系统统计函数 、文本和图像函数--收藏着有用

    行级函数:下列行集函数将返回一个可用于代替 Transact-SQL 语句中表引用的对象. CONTAINSTABLE 返回具有零行.一行或多行的表,这些行的列中包含的基于字符类型的数据是单个词语和短 ...

  5. 攻城利器 —— Carthage简单介绍

    01 Carthage简单介绍 主页:https://github.com/Carthage/Carthage.git 作者:Justin Spahr-Summers等 版本:0.18 目标:用最简单 ...

  6. Mac OS X 系统12个常用的文本编辑快捷键(移动、选中)

    经常和文字处理打交道?如果多多使用下面这 12 个快捷键,在移动.选择.复制等操作文字时效率会大大提升. 6 个移动光标的快捷键第一组快捷键可以用来在文本中快速移动光标: 跳到本行开头 – Comma ...

  7. android 5.0 webview坑

    http://www.heapoverflow.me/question-webview-loadurlurl-headers-not-working-in-android-31203905 It's ...

  8. select选择框内容左右移动添加删除栏(升级)

    先看一下之前的版本(10年前的作品了) 新版增加了拖动事件(双向及本列),双击左右自动移动,修正了算法性能更好: 也更新了如果姓名长度太长显示变形问题

  9. Remove WebCakeDesktop

    WebCakeDesktop.Updater.exe 是广告程序,卸载步骤参考 http://malwaretips.com/blogs/webcake-desktop-updater-exe-rem ...

  10. easyui中 在子tabs中 添加新的tabs

    function addToParentTab(title, url) {            self.parent.addTabIgnoreExist(title, url, 'icon-cha ...