gpt4 book ai didi

.net - 属性参数必须是一个常量表达式

转载 作者:行者123 更新时间:2023-12-03 07:29:09 24 4
gpt4 key购买 nike

以下问题是否有任何解决方法?

An attribute argument must be a constant expression



我想在属性的参数中使用小数。

最佳答案

不幸的是,您不能在属性值中使用小数,因为CLR本身并不真正了解System.Decimal类型-它不是像int,double等原始类型。C#编译器基本上将其伪装为小数类型的const字段,但是使用属性无法达到相同的效果。

根据C#3规范的17.1.3节:

The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:

  • One of the following types: bool, byte, char, double, float, int, long, sbyte, short, string, uint, ulong, ushort.
  • The type object.
  • The type System.Type.
  • An enum type, provided it has public accessibility and the types in which it is nested (if any) also have public accessibility (§17.2).
  • Single-dimensional arrays of the above types.


然后在第17.2节中:

An expression E is an attribute-argument-expression if all of the following statements are > true:

  • The type of E is an attribute parameter type (§17.1.3).
  • At compile-time, the value of E can be resolved to one of the following:
    • A constant value.
    • A System.Type object.
    • A one-dimensional array of attribute-argument-expressions.

关于.net - 属性参数必须是一个常量表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2763981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com