0
我想實現類似於IntegerAboveThresholdAttribute的東西,除了它應該與小數工作。試圖實現與問題的DecimalAboveThresholdAttribute
這是使用它作爲一個BusinessException
[DecimalAboveThreshold(typeof(BusinessException), 10000m, ErrorMessage = "Dollar Value must be 10000 or lower.")]
不過,我收到一個錯誤說的屬性必須是常量表達式,typeof運算表達式或屬性參數類型的數組創建表達式我實現了我的。我想知道是否有可能解決這個問題,如果沒有,是否有可能做類似的事情?
下面是DecimalAboveThresholdAttribute的源代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreLib.Messaging;
namespace (*removed*)
{
public class DecimalBelowThresholdAttribute : BusinessValidationAttribute
{
private decimal _Threshold;
public DecimalBelowThresholdAttribute(Type exceptionToThrow, decimal threshold)
: base(exceptionToThrow)
{
_Threshold = threshold;
}
protected override bool Validates(decimal value)
{
return (decimal)value < _Threshold;
}
}
}
我也想知道如果我能做到這一點與DateTime是否爲好。