且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么是“十进制"?不是有效的属性参数类型?

更新时间:2023-02-14 21:06:07

这是一个 CLR 限制.仅有的原始常量或数组原语可以用作属性参数.原因是一个属性必须完全编码在元数据.这不同于一个用 IL 编码的方法体.使用元数据只会严格限制可以使用的值的范围.在当前版本的 CLR 中,元数据值仅限于原语、null、类型和数组原语(可能错过了一个未成年人一).

This is a CLR restriction. Only primitive constants or arrays of primitives can be used as attribute parameters. The reason why is that an attribute must be encoded entirely in metadata. This is different than a method body which is coded in IL. Using MetaData only severely restricts the scope of values that can be used. In the current version of the CLR, metadata values are limited to primitives, null, types and arrays of primitives (may have missed a minor one).

取自 this 答案JaredPar.

基本类型不是小数原始类型,因此不能以元数据表示,可防止它不是一个属性参数.

Decimals while a basic type are not a primitive type and hence cannot be represented in metadata which prevents it from being an attribute parameter.