RangeValidator无法使用十进制值(c#,Enterprise Library)

时间:2014-04-27 22:37:01

标签: c# wpf validation enterprise-library

Hello Stackoverflow fellas!

我正在使用Enterprise Library 5.0,我正在尝试验证十进制值是否来自指定范围,但是没有明显的方法可以做到这一点

例如

[RangeValidator(0, RangeBoundaryType.Exclusive, 1, RangeBoundaryType.Ignore)]  

仅适用于int,double和amp;浮。

我正在寻找等效的Visual Basic代码行:

<RangeValidator(GetType(Decimal), "0.00", RangeBoundaryType.Inclusive, "0.00", RangeBoundaryType.Ignore, MessageTemplate := "Value must be greater than 0.")>

我不想投,因为精确。 有没有人克服过这个?

3 个答案:

答案 0 :(得分:0)

我不确定这是否有效,但请尝试将范围值设为十进制:

[RangeValidator(0.0, RangeBoundaryType.Exclusive, 1.0, RangeBoundaryType.Ignore)]

如果这不起作用,请尝试:

[RangeValidator(0.0m, RangeBoundaryType.Exclusive, 1.0m, RangeBoundaryType.Ignore)]

答案 1 :(得分:0)

RangeValudatorAttribute类没有直接支持小数的构造函数 - VB可以间接地转换

您可以使用取double值的constructor代替:

[RangeValidator(0.0, RangeBoundaryType.Exclusive, 1.0, RangeBoundaryType.Ignore)]  

由于0.01.0可以用二进制表示完全,因此在与decimal值进行比较时,不应出现精度问题。

答案 2 :(得分:0)

我已经用以下十进制方式解决了它:

[RangeValidator(typeof(decimal), "0.00", RangeBoundaryType.Inclusive, "1.00", RangeBoundaryType.Ignore)]