我已创建一个自定义属性:
[AttributeUsage(AttributeTargets.Method| AttributeTargets.Class)]
public class ActionAttribute : ActionFilterAttribute
{
public int Id { get; set; }
public string Work { get; set; }
}
我的控制器:
[Area("Administrator")]
[Action(Id = 100, Work = "Test")]
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
我的代码:我使用反射在当前程序集中查找所有的控制器。
Assembly.GetEntryAssembly()
.GetTypes()
.AsEnumerable()
.Where(type => typeof(Controller).IsAssignableFrom(type))
.ToList()
.ForEach(d =>
{
// how to get ActionAttribute ?
});
是否有可能以编程方式读取所有的ActionAttribute
?
Assembly.GetEntryAssembly()
会获取用作入口的程序集。因此,在例如单元测试中,其行为将不同。 - Sander Visser