本文介绍了一个属性参数必须是恒定的前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there any workaround for the following issue?

这是属性参数必须是一个常量前pression

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

I want to use decimals in an attribute’s parameter.

推荐答案

不幸的是你不能使用小数的属性值,因为CLR本身并不真正了解 System.Decimal 类型 - 它不是一个基本类型如int,双等C#编译器基本上是假货它decimal类型const的领域,但它无法实现与属性相同的效果

Unfortunately you can't use decimals in attribute values, as the CLR itself doesn't really know about System.Decimal type - it's not a primitive type like int, double etc. The C# compiler basically fakes it for const fields of type decimal, but it can't achieve the same effect with attributes.

从C#3规格,第17.1.3:

From the C# 3 spec, section 17.1.3:

的定位和命名的类型  一个属性类的参数  有限的属性参数  类型,它们是:

      
  • 执行下列操作之一  类型:布尔,字节,字符,双,  浮动,INT,长,为sbyte,总之,  串,UINT,ULONG,USHORT。
  •   
  • 类型的对象。
  •   
  • 类型的System.Type。
  •   
  • 枚举  类型,只要它具有公共  可访问性和类型,其中  它是嵌套(如果有的话)也具有公共  可访问性(第17.2节)。
  •   
  • 在上面的一维数组  类型。
  •   
  • 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:

这是EX pression E是一个属性参数EX pression如果所有下面的语句是>真:

      
  • e的类型是一个属性  参数类型(§17.1.3)。
  •   
  • 在  编译时,E的值可以是  解析为以下之一:  
        
    • 系统  恒定值。
    •   
    • 一个System.Type对象。
    •   到
    • 一维数组  属性参数EX pressions。
    •   
    • 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.

      这篇关于一个属性参数必须是恒定的前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 18:06
查看更多