NHibernate DecimalMin 验证器

发布于 2024-10-03 17:43:25 字数 480 浏览 1 评论 0原文

我正在尝试使用 NHibernates Validator 库中的 DecimalMin 验证器。不幸的是,由于编译器的限制,我在尝试使用它时遇到了错误。我确信这很简单,但我现在需要转移到另一项任务,并且不再在上面浪费时间,因此我们将不胜感激。

    [DecimalMin(10.01)]
    public decimal Amount { get; set; }

上面的代码使用构造函数值为 10.01 的 DecimalMin 属性。属性的构造函数接受十进制值。但是,编译器不允许 10.01 或 10.01m。

以 10.01 作为我收到的值:“无法将源类型‘double’转换为目标类型‘decimal’

以 10.01m 作为我收到的值:“属性参数必须是常量表达式、表达式类型或数组创建表达式属性参数类型。”

我用谷歌搜索试图找到答案,但今天我的谷歌搜索失败了。

有人有如何使用此验证器的示例吗?

I'm attempting to use the DecimalMin validator from NHibernates Validator library. Unfortunately, due to compiler restrictions I'm getting errors when attempting to utilize it. I'm sure its something simple, but I'm at a point where I need to move onto another task and not burn anymore time on it so any help would be appreciated.

    [DecimalMin(10.01)]
    public decimal Amount { get; set; }

The code above uses the DecimalMin attribute with the constructor value of 10.01. The constructor of the Attribute accepts decimal values. However, the compiler will not allow 10.01 or 10.01m.

With 10.01 as the value I receive: "Cannot convert source type 'double' to target type 'decimal'

With 10.01m as the value I receive: "An attribute argument must be a constant expression, type of expression or array creation expression of an attribute parameter type."

I've googled around to try and find an answer and my google-fu is failing me today.

Does anyone have an example of how to use this validator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2024-10-10 17:43:25

这是 NHibernate.Validator 中的一个错误。小数在属性参数中不可表示。

在 C# 中使用十进制值作为属性参数?

解决方案是修补 NHibernate.Validator 以便构造函数采用 float 或 double,然后在 ctor 中执行到十进制的转换。希望我能为您提供更好的答案。

This is a bug in NHibernate.Validator. Decimals are not representable in attribute parameters.

use decimal values as attribute params in c#?

The solution is to patch NHibernate.Validator so that the constructor takes a float or double and then perform the conversion to decimal in the ctor. Wish I had a better answer for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文