C# 尝试实现DecimalAboveThresholdAttribute时出现问题

C# 尝试实现DecimalAboveThresholdAttribute时出现问题,c#,asp.net,validation,exception,business-rules,C#,Asp.net,Validation,Exception,Business Rules,我正在尝试实现一些类似于IntegerableOverThresholdAttribute的东西,只是它应该使用小数 这是我将其用作业务异常的实现 [DecimalAboveThreshold(typeof(BusinessException), 10000m, ErrorMessage = "Dollar Value must be 10000 or lower.")] 但是,我收到一个错误,指出属性必须是常量表达式、typeof表达式或属性参数类型的数组创建表达式。我想知道是否有可能解决这

我正在尝试实现一些类似于IntegerableOverThresholdAttribute的东西,只是它应该使用小数

这是我将其用作业务异常的实现

[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;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用CoreLib.Messaging;
命名空间(*已删除*)
{
公共类小数低于ThresholdAttribute:BusinessValidationAttribute
{
私有十进制阈值;
public DecimalBelowThresholdAttribute(类型ExceptionTorRow,十进制阈值)
:基础(投掷除外)
{
_阈值=阈值;
}
受保护的覆盖布尔验证(十进制值)
{
返回(十进制)值<\u阈值;
}
}
}


我还想知道我是否也可以用DateTimes执行此操作。

不允许使用小数作为属性参数。这是.NET属性中的内置限制。您可以在上找到可用的参数类型。所以它不适用于十进制和日期时间。作为一种解决方法(尽管它不是类型安全的),您可以使用字符串:

public DecimalBelowThresholdAttribute(Type exceptionToThrow, string threshold)
        : base(exceptionToThrow)
    {
        _Threshold = decimal.Parse(threshold);
    }
用法:

[DecimalAboveThreshold(typeof(BusinessException), "10000", ErrorMessage = "Dollar Value must be 10000 or lower.")]