与字节一起使用RangeValidator [英] Using RangeValidator with byte

查看:244
本文介绍了与字节一起使用RangeValidator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是相关的属性声明:

 [RangeValidator(1,RangeBoundaryType.Inclusive,255,RangeBoundaryType.Inclusive,MessageTemplate = "StartFlexibility is out of range")]
    public byte StartFlexibility { get; set; }

调用validate方法时,抛出FormatException告诉我值类型必须为Int32。

When the validate method is called, a FormatException is thrown telling me that the value type needs to be Int32.

如何解决?

推荐答案

好。 ..快速明显的解决方法是将类型更改为short或int,

well... the quick obvious fix will be change the type to short or int,

但我要执行的另一项观察是使用范围。您正在告诉RangeValidator接受一个介于1和256之间的范围,但是您只能分配一个字节值,直到255,这也许是编译器大声疾呼的原因。

but another observation i want to do, is with the range. You are telling the RangeValidator to take a inclusive range between 1 and 256, but you just can assign a byte value till 255, maybe that's the compiler reason to cry out.

RangeValidator还会从参数中推断Range的类型,因此,请尝试投射

The RangeValidator is also infering the type of the Range from the parameters, so, try casting

[RangeValidator((byte) 1, ...

这篇关于与字节一起使用RangeValidator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆