我正在尝试实现类似于 IntegerAboveThresholdAttribute 的东西,除了它应该使用小数。
这是我将其用作 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;
}
}
}
我也想知道我是否也可以使用 DateTimes 来做到这一点。