问题描述
C# 属性如何可能在其名称中包含Attribute"(例如 DataMemberAttribute
)但在没有此后缀的情况下进行了初始化?例如:
[DataMember]私人诠释我;
根据C# 语言规范,
<块引用>按照惯例,属性类以Attribute
后缀命名.type-name 形式的 attribute-name 可以包含或省略此后缀.
这是 C# 编译器提供的快捷方式,绝不是 CLR 功能.编译器对属性进行特殊处理的另一个示例是 ObsoleteAttribute属性:此属性强制编译器发出警告/错误,但它对 CLR 没有特殊意义.
至于属性是如何解析的,请看上面的链接.总结一下:
<块引用>如果找到带有和不带有此后缀的属性类,则存在歧义,并导致编译时错误.如果 attribute-name
的拼写使得其最右边的标识符是 逐字标识,则只匹配不带后缀的属性,从而解决这种歧义.
逐字标识符"是带有 @
前缀的标识符.
继续使用 MSDN:
使用系统;[AttributeUsage(AttributeTargets.All)]公共类 X:属性{}[AttributeUsage(AttributeTargets.All)]公共类XAttribute:属性{}[X]//错误:歧义类 Class1 {}[XAttribute]//指 XAttribute类 Class2 {}[@X]//指 X类 Class3 {}[@XAttribute]//指 XAttribute类 Class4 {}
<块引用>
属性 [X]
不明确,因为它可以引用 X
或 XAttribute
.使用逐字标识符允许在这种罕见的情况下指定确切的意图.属性 [XAttribute]
没有歧义(尽管如果有一个名为 XAttributeAttribute
的属性类就会有歧义!).如果删除了类 X
的声明,则两个属性都引用名为 XAttribute
的属性类,如下所示:
使用系统;[AttributeUsage(AttributeTargets.All)]公共类XAttribute:属性{}[X]//指 XAttribute类 Class1 {}[XAttribute]//指 XAttribute类 Class2 {}[@X]//错误:没有名为X"的属性类 Class3 {}
How is it possible that C# attributes have "Attribute" in their name (e.g. DataMemberAttribute
) but are initialized without this suffix? e.g.:
[DataMember]
private int i;
According to the C# Language Specification,
By convention, attribute classes are named with a suffix of
Attribute
. An attribute-name of the form type-name may either include or omit this suffix.
This is a shortcut provided by the C# compiler and by no means a CLR feature. Another example of special treatment of attributes by the compiler is an ObsoleteAttribute attribute: this one forces a compiler to issue a warning/error, but it has no special meaning for the CLR.
As for how attributes are resolved, see the link above. To sum it up:
If an attribute class is found both with and without this suffix, an ambiguity is present, and a compile-time error results. If the
attribute-name
is spelled such that its right-most identifier is a verbatim identifier, then only an attribute without a suffix is matched, thus enabling such an ambiguity to be resolved.
A "verbatim identifier" is an identifier with an @
prefix.
Continuing with MSDN:
using System;
[AttributeUsage(AttributeTargets.All)]
public class X: Attribute
{}
[AttributeUsage(AttributeTargets.All)]
public class XAttribute: Attribute
{}
[X] // Error: ambiguity
class Class1 {}
[XAttribute] // Refers to XAttribute
class Class2 {}
[@X] // Refers to X
class Class3 {}
[@XAttribute] // Refers to XAttribute
class Class4 {}
The attribute
[X]
is ambiguous, since it could refer to eitherX
orXAttribute
. Using a verbatim identifier allows the exact intent to be specified in such rare cases. The attribute[XAttribute]
is not ambiguous (although it would be if there was an attribute class namedXAttributeAttribute
!). If the declaration for classX
is removed, then both attributes refer to the attribute class namedXAttribute
, as follows:
using System;
[AttributeUsage(AttributeTargets.All)]
public class XAttribute: Attribute
{}
[X] // Refers to XAttribute
class Class1 {}
[XAttribute] // Refers to XAttribute
class Class2 {}
[@X] // Error: no attribute named "X"
class Class3 {}
这篇关于C#属性名缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!