我编写了一个自定义操作方法选择器属性,它有三个bool
属性。这三个属性都为false
是无效的。至少其中一个必须为true
。当执行IsValidForRequest
时,我会检查它们中至少有一个是true
。但如果没有,我应该抛出哪个异常呢?
一些相关的代码:
public class MyCustomAttribute : ActionMethodSelectorAttribute
{
public bool Prop1 { get; set; }
public bool Prop2 { get; set; }
public bool Prop3 { get; set; }
public MyCustomAttribute()
{
this.Prop1 = true;
this.Prop2 = true;
this.Prop3 = true;
}
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
// at least one of them must be true
if (!this.Prop1 && !this.Prop2 && !this.Prop3)
{
throw new ?????
}
// other codez here
}
}
属性有一个很棒的功能,既可以初始化它们,又可以提供属性值,因此我必须在IsValidForRequest
方法中检查它们。
[MyCustom(Prop1 = false, Prop2 = false, Prop3 = false)]
InvalidOperationException
,但每次使用它时,我总是想知道是否有更好的选择! - Danny Tuppeny