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

  • <tfoot id='tQZNB'></tfoot>

    <legend id='tQZNB'><style id='tQZNB'><dir id='tQZNB'><q id='tQZNB'></q></dir></style></legend>
    <i id='tQZNB'><tr id='tQZNB'><dt id='tQZNB'><q id='tQZNB'><span id='tQZNB'><b id='tQZNB'><form id='tQZNB'><ins id='tQZNB'></ins><ul id='tQZNB'></ul><sub id='tQZNB'></sub></form><legend id='tQZNB'></legend><bdo id='tQZNB'><pre id='tQZNB'><center id='tQZNB'></center></pre></bdo></b><th id='tQZNB'></th></span></q></dt></tr></i><div id='tQZNB'><tfoot id='tQZNB'></tfoot><dl id='tQZNB'><fieldset id='tQZNB'></fieldset></dl></div>
        <bdo id='tQZNB'></bdo><ul id='tQZNB'></ul>
      1. 为 .NET 属性目标指定所需的基类

        Specify required base class for .NET attribute targets(为 .NET 属性目标指定所需的基类)
          <tbody id='GX73k'></tbody>

        1. <tfoot id='GX73k'></tfoot>
          • <bdo id='GX73k'></bdo><ul id='GX73k'></ul>

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

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

                  <legend id='GX73k'><style id='GX73k'><dir id='GX73k'><q id='GX73k'></q></dir></style></legend>
                • 本文介绍了为 .NET 属性目标指定所需的基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试使用下面的代码创建自定义 .NET 属性,但不小心遗漏了子类.这产生了注释中显示的易于修复的编译器错误.

                  I tried to create a custom .NET attribute with the code below but accidentally left off the subclass. This generated an easily-fixed compiler error shown in the comment.

                  // results in compiler error CS0641: Attribute 'AttributeUsage' is 
                  // only valid on classes derived from System.Attribute
                  [AttributeUsage(AttributeTargets.Class)]
                  internal class ToolDeclarationAttribute
                  {
                      internal ToolDeclarationAttribute()
                      {
                      }
                  }
                  

                  我的问题是编译器如何知道 [AttributeUsage] 属性只能应用于 System.Attribute 的子类?使用 .NET Reflector 我没有看到 AttributeUsageAttribute 类声明本身有什么特别之处.不幸的是,这可能只是编译器本身生成的一种特殊情况.

                  My question is how does the compiler know the [AttributeUsage] attribute can only be applied to a subclass of System.Attribute? Using .NET Reflector I don't see anything special on the AttributeUsageAttribute class declaration itself. Unfortunately this might just be a special case generated by the compiler itself.

                  [Serializable, ComVisible(true), AttributeUsage(AttributeTargets.Class, Inherited=true)]
                  public sealed class AttributeUsageAttribute : Attribute
                  {
                      ...
                  

                  我希望能够指定我的自定义属性只能放置在特定类(或接口)的子类上.这可能吗?

                  I would like to be able to specify that my custom attribute can only be placed on subclasses of a particular class (or interface). Is this possible?

                  推荐答案

                  我希望能够指定我的自定义属性只能放置在特定类(或接口)的子类上.这可能吗?

                  I would like to be able to specify that my custom attribute can only be placed on subclasses of a particular class (or interface). Is this possible?

                  实际上,有一种方法可以使用 protected 对子类(但不是接口)执行此操作 - 请参阅 限制属性使用.重现代码(但不是讨论):

                  Actually, there is a way to do this for subclasses (but not interfaces) using protected - see Restricting Attribute Usage. To reproduce the code (but not the discussion):

                  abstract class MyBase {
                      [AttributeUsage(AttributeTargets.Property)]
                      protected sealed class SpecialAttribute : Attribute {}
                  }
                  class ShouldBeValid : MyBase {
                      [Special] // works fine
                      public int Foo { get; set; }
                  }
                  class ShouldBeInvalid { // not a subclass of MyBase
                      [Special] // type or namespace not found
                      [MyBase.Special] // inaccessible due to protection level
                      public int Bar{ get; set; }
                  }
                  

                  这篇关于为 .NET 属性目标指定所需的基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                  Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)

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

                  • <legend id='TwiJQ'><style id='TwiJQ'><dir id='TwiJQ'><q id='TwiJQ'></q></dir></style></legend>

                        <tfoot id='TwiJQ'></tfoot>
                          <tbody id='TwiJQ'></tbody>

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

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