在 C# 中使用十进制值作为属性参数? [英] use decimal values as attribute params in c#?

查看:34
本文介绍了在 C# 中使用十进制值作为属性参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用十进制值作为字段属性的参数,但出现编译器错误.

I've been trying to use decimal values as params for a field attribute but I get a compiler error.

我发现这篇博文 link 说那时不可能在 .NET 中使用,有人知道他们为什么选择这个或者我如何使用十进制参数吗?

I found this blog post link saying it wasn't possible in .NET to use then, does anybody know why they choose this or how can I use decimal params?

推荐答案

这是一个 CLR 限制.只有原始常量或原始数组可以用作属性参数.原因是属性必须完全在元数据中编码.这与用 IL 编码的方法体不同.使用 MetaData 只会严重限制可以使用的值的范围.在当前版本的 CLR 中,元数据值仅限于基元、空值、类型和基元数组(可能漏掉了一个次要的).

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).

小数虽然是基本类型,但不是原始类型,因此不能在元数据中表示,这阻止了它成为属性参数.

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.

这篇关于在 C# 中使用十进制值作为属性参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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