C#属性参数类型:是否允许其他属性? [英] C# attribute parameter type: does it allow another Attribute?
问题描述
我是对使用C#感兴趣的开发人员.我感兴趣的功能是Attribute(在Java中称为注释).
I am a developer interested in working with C#. The feature I'm interested in is Attribute (called annotation in Java).
我的问题是:属性参数类型是否支持使用其他属性?
My question is: Does attribute parameter type support the use of another Attribute?
在C#语言规范的第17.1.3节中,其规定如下:
In Section 17.1.3 of the C# language specification, it states the followings:
" ...
"...
属性类的位置和命名参数的类型限于以下属性参数类型:
The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:
- 以下类型之一:bool,byte,char,double,float,int,long,sbyte,short,string,uint,ulong,ushort.
- 类型对象.
- 类型System.Type.
- 枚举类型,只要它具有公共可访问性,并且嵌套的类型(如果有)也具有公共可访问性(第17.2节).
- 上述类型的一维数组.
没有这些类型之一的构造函数参数或公共字段不能用作属性规范中的位置参数或命名参数.
A constructor argument or public field which does not have one of these types, cannot be used as a positional or named parameter in an attribute specification.
..."
..."
我很好奇第二种类型(类型对象)是否还意味着可以使用Attribute.因为据我所知,Object是所有引用类型(?)的超类型.
I am curious whether the second type (type object) also means that Attribute can be used. Because, as far as I know Object is the supertype of all referenced types (?).
为了说明,我想实现以下目标:
To illustrate, I want to achieve something like the following:
[MyAttribute("abc",p1 ="...", p2 = [AnotherAttribute("123")] )]
[ MyAttribute( "abc", p1="...", p2=[ AnotherAttribute("123") ] ) ]
class MyClass { ...}
class MyClass { ... }
其中:MyAttribute和AnotherAttribute是属性,参数MyAttribute.p2声明为AnotherAttribute类型.
where: MyAttribute and AnotherAttribute are attributes, and parameter MyAttribute.p2 is declared with the type AnotherAttribute.
非常感谢您的回复.
推荐答案