1、获取控制器所有Action:
Type t = typeof(StatController);//StatController是指定控制器的名称
System.Reflection.MethodInfo[] ControllerMethods = t.GetMethods();
StringBuilder methodsNameAppend = new StringBuilder();
for (int i = 0; i < ControllerMethods.Length; i++)
{
methodsNameAppend.Append(ControllerMethods[i].Name + ";");
}2、获取备注: [DescriptionName("应收费录入内容")]定义一个过滤器表头
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Soarway.WYGL.API.Controllers
{
class DescriptionNameAttribute : Attribute
{
public string DescriptionName { get;set;}
public DescriptionNameAttribute(string name)
{
this.DescriptionName=name;
}
}
}
3、实例:ControllerMethods[8].GetCustomAttributesData()[1].ConstructorArguments[0].Value获取过滤器名称
本文介绍如何使用C#反射技术获取指定控制器的所有Action方法,并通过自定义属性获取这些方法上的描述信息。该技术适用于.NET框架下对控制器进行自动化操作或生成文档等场景。
152

被折叠的 条评论
为什么被折叠?



