0
我正在創建屬性,我需要傳遞異常數組,如何做到這一點?如何將異常數組傳遞給屬性
比方說
[assembly: MyAttribute(ExceptionList = [System.Web.HttpException, System.Threading.ThreadAbortException]);
我正在創建屬性,我需要傳遞異常數組,如何做到這一點?如何將異常數組傳遞給屬性
比方說
[assembly: MyAttribute(ExceptionList = [System.Web.HttpException, System.Threading.ThreadAbortException]);
如果你想傳遞的異常類型那麼你可以使用typeof
:
[assembly: MyAttribute(ExceptionList = [typeof(System.Web.HttpException), typeof(System.Threading.ThreadAbortException]));
如果你想傳遞例外對象,這是不可能的。屬性構造函數的參數只能是常量值(或者是一些特殊例外的類型的表達式)。
您需要傳遞類型列表而不是異常。這應該讓你開始吧
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class MyAttribute : Attribute {
public MyAttribute(Type[] exceptionList) {
}
}
[MyAttribute(new Type[] { typeof(ArgumentException), typeof(OtherException) })]
class Test {
...
}