我怎么能把List解析成自定义动作过滤器(比如输入参数)?
public class CustomFilter : ActionFilterAttribute { public List<MyEnumType> InputParameter { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); } } [CustomFilter(InputParameter = new List<MyEnumType>() { MyEnumType.Type } )] public SomeActionInController() { }
我得错了错误
'InputParameter' is not a valid named attribute argument because it is not a valid attribute parameter type
解决方法
操作过滤器参数是操作过滤器的属性:
[CustomFilter(InputParameter=10)] public SomeActionInController() { } public class CustomFilter : ActionFilterAttribute { public int InputParameter { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { // access this.InputParameter base.OnActionExecuting(filterContext); } }
属性参数类型仅限于此处描述的类型 – http://msdn.microsoft.com/en-us/library/aa664615%28v=vs.71%29.aspx
您可以通过属性构造函数传递集合,如此处所述 – Can I initialize a C# attribute with an array or other variable number of arguments?