与字节一起使用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, ...