【发布时间】:2015-08-07 09:47:53
【问题描述】:
基于this article,我正在尝试为 ASP.NET Core 创建一个IActionFilter 实现,它可以处理控制器上标记的属性和控制器的操作。虽然读取控制器的属性很容易,但我无法找到读取动作方法上定义的属性的方法。
这是我现在拥有的代码:
public sealed class ActionFilterDispatcher : IActionFilter
{
private readonly Func<Type, IEnumerable> container;
public ActionFilterDispatcher(Func<Type, IEnumerable> container)
{
this.container = container;
}
public void OnActionExecuting(ActionExecutingContext context)
{
var attributes = context.Controller.GetType().GetCustomAttributes(true);
attributes = attributes.Append(/* how to read attributes from action method? */);
foreach (var attribute in attributes)
{
Type filterType = typeof(IActionFilter<>).MakeGenericType(attribute.GetType());
IEnumerable filters = this.container.Invoke(filterType);
foreach (dynamic actionFilter in filters)
{
actionFilter.OnActionExecuting((dynamic)attribute, context);
}
}
}
public void OnActionExecuted(ActionExecutedContext context)
{
throw new NotImplementedException();
}
}
我的问题是:如何在 ASP.NET Core MVC 中读取 action 方法的属性?
【问题讨论】:
-
你会得到
MemberInfo,不管你有兴趣使用反射API,然后使用GetCustomAttributes。希望我没有误解这个问题
标签: c# .net asp.net-core asp.net-core-mvc