C#属性名称缩写缩写、属性、名称

2023-09-02 10:32:33 作者:你的脸红了耶

这怎么可能是C#属性具有属性,在他们的名字(比如 DataMemberAttribute ),但没有这个后缀初始化?例如:

  [数据成员]
私人诠释我;
 

解决方案

根据该的 C#语言规范,

  

根据约定,属性类的名称与属性的后缀。一个的属性名的形式的类型名称的可能包含或省略此后缀。

这是由C#编译器和绝不是一个CLR功能提供了一条捷径。由编译器属性的特殊处理的另一个例子是 ObsoleteAttribute 属性:这个力量一个的编译器的发出警告/错误,但它已为CLR没有任何特殊含义。

至于如何属性都解决了,看到上面的链接。概括起来:

  DNF本次五一礼包奶爸奶妈成为最大输家,侧滑你赢了

如果属性类的发现,有和没有这个后缀,多义性是present和编译时错误的结果。如果属性名拼写使得它的最右边的标识符是的逐字标识符的,那么只有没有后缀的属性相匹配,从而使这样的歧义待解决。

一个逐字标识符是一个标识符的 @ preFIX。

通过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 either X or XAttribute. 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 named XAttributeAttribute!). If the declaration for class X is removed, then both attributes refer to the attribute class named XAttribute, 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 {}