<small id='sLdHm'></small><noframes id='sLdHm'>

    <bdo id='sLdHm'></bdo><ul id='sLdHm'></ul>

    1. <legend id='sLdHm'><style id='sLdHm'><dir id='sLdHm'><q id='sLdHm'></q></dir></style></legend>

      1. <i id='sLdHm'><tr id='sLdHm'><dt id='sLdHm'><q id='sLdHm'><span id='sLdHm'><b id='sLdHm'><form id='sLdHm'><ins id='sLdHm'></ins><ul id='sLdHm'></ul><sub id='sLdHm'></sub></form><legend id='sLdHm'></legend><bdo id='sLdHm'><pre id='sLdHm'><center id='sLdHm'></center></pre></bdo></b><th id='sLdHm'></th></span></q></dt></tr></i><div id='sLdHm'><tfoot id='sLdHm'></tfoot><dl id='sLdHm'><fieldset id='sLdHm'></fieldset></dl></div>
        <tfoot id='sLdHm'></tfoot>

        C#属性名缩写

        时间:2023-07-27
        • <bdo id='UYACT'></bdo><ul id='UYACT'></ul>

          <small id='UYACT'></small><noframes id='UYACT'>

            <tfoot id='UYACT'></tfoot>

                  <tbody id='UYACT'></tbody>

                1. <legend id='UYACT'><style id='UYACT'><dir id='UYACT'><q id='UYACT'></q></dir></style></legend>

                2. <i id='UYACT'><tr id='UYACT'><dt id='UYACT'><q id='UYACT'><span id='UYACT'><b id='UYACT'><form id='UYACT'><ins id='UYACT'></ins><ul id='UYACT'></ul><sub id='UYACT'></sub></form><legend id='UYACT'></legend><bdo id='UYACT'><pre id='UYACT'><center id='UYACT'></center></pre></bdo></b><th id='UYACT'></th></span></q></dt></tr></i><div id='UYACT'><tfoot id='UYACT'></tfoot><dl id='UYACT'><fieldset id='UYACT'></fieldset></dl></div>
                  本文介绍了C#属性名缩写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  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] 不明确,因为它可以引用 XXAttribute.使用逐字标识符允许在这种罕见的情况下指定确切的意图.属性 [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 {}
                  

                  这篇关于C#属性名缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:你能用“where"吗?在 c# 中需要一个属性? 下一篇:如何将我的属性添加到代码生成的 Linq2Sql 类属性?

                  相关文章

                3. <legend id='GkXyJ'><style id='GkXyJ'><dir id='GkXyJ'><q id='GkXyJ'></q></dir></style></legend>

                4. <small id='GkXyJ'></small><noframes id='GkXyJ'>

                5. <i id='GkXyJ'><tr id='GkXyJ'><dt id='GkXyJ'><q id='GkXyJ'><span id='GkXyJ'><b id='GkXyJ'><form id='GkXyJ'><ins id='GkXyJ'></ins><ul id='GkXyJ'></ul><sub id='GkXyJ'></sub></form><legend id='GkXyJ'></legend><bdo id='GkXyJ'><pre id='GkXyJ'><center id='GkXyJ'></center></pre></bdo></b><th id='GkXyJ'></th></span></q></dt></tr></i><div id='GkXyJ'><tfoot id='GkXyJ'></tfoot><dl id='GkXyJ'><fieldset id='GkXyJ'></fieldset></dl></div>
                      • <bdo id='GkXyJ'></bdo><ul id='GkXyJ'></ul>
                      <tfoot id='GkXyJ'></tfoot>