C# 特性AttributeUsage简介与使用教程

作者:wu.g.q 时间:2022-09-30 01:53:21 

AttributeUsage

预定义特性AttributeUsage描述了如何使用一个自定义特性类。它规定了特性可应用到的项目的类型。

规定该特性的语法如下:

[AttributeUsage(
   validon,
   AllowMultiple=allowmultiple,
   Inherited=inherited
)]

validon:自定义特性的对象,可以是类、方法、属性等对象(默认值是 AttributeTargets.All)
AllowMultiple:是否允许被多次使用(默认值为false:单用的)
Inherited:是否可被派生类继承(默认值为false:不能)

下面请看使用:

using System;
namespace AttributeUsagePractice
{
   [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
   public class HelpAttribute : Attribute
   {
       public HelpAttribute(String Description_in)
       {
           this.description = Description_in;
       }
       protected String description;
       public String Description
       {
           get { return this.description; }
       }
   }
   class Program
   {
       [Help("this is a main class")]//error
       public static void Main(string[] args)
       {
           Console.WriteLine("Hello World!");
           // TODO: Implement Functionality Here
           Console.Write("Press any key to continue . . . ");
           Console.ReadKey(true);
       }
   }
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]

含义为:定制特性类,不允许多次使用,不能被继承

第一个参数:
因为它的特性目标是 AttributeTargets.Class,而它放在函数前面,所以上面的程序会报错:特性“Help”对此声明类型无效。它只对“class”声明有效,正确的做法是放在 class Program 上面。

如果是下面的代码:

using System;
namespace AttributeUsagePractice
{
   [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
   public class HelpAttribute : Attribute
   {
       public HelpAttribute(String Description_in)
       {
           this.description = Description_in;
       }
       protected String description;
       public String Description
       {
           get { return this.description; }
       }
   }
   [Help("this is a main class")]
   [Help("this is a main2 class")]//error
   class Program
   {
       public static void Main(string[] args)
       {
           Console.WriteLine("Hello World!");
           // TODO: Implement Functionality Here
           Console.Write("Press any key to continue . . . ");
           Console.ReadKey(true);
       }
   }
}

第二个参数:

因为AllowMultiple = false,上面多次使用,所以报错 重复的“Help”特性,正确的做法就是去掉它

using System;
using System.Linq;
namespace AttributeUsagePractice
{
   [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
   public class HelpAttribute : Attribute
   {
       public HelpAttribute(){}
       public HelpAttribute(String Description_in)
       {
           this.description = Description_in;
       }
       protected String description;
       public String Description
       {
           get { return this.description; }
       }
   }
   [Help("this is a HelpAttribute use class")]
   public class UseHelpAttribute
   {
   }
   public class UseHelpAttributeDerive : UseHelpAttribute
   {
   }
   class Program
   {
       public static void Main(string[] args)
       {
           // TODO: Implement Functionality Here
           UseHelpAttributeDerive objHelpAttribute = new UseHelpAttributeDerive();
           Type t = objHelpAttribute.GetType();
           object [] objAttrs = t.GetCustomAttributes(typeof(HelpAttribute),true);
           if(objAttrs!= null && objAttrs.Length > 0)
           {
               object temp = objAttrs.First();
               HelpAttribute myAttr = temp as HelpAttribute;
               Console.WriteLine("类描述:{0}", myAttr.Description);
           }
           else
           {
               Console.WriteLine("没有类描述");
           }
           Console.ReadKey(true);
       }
   }
}

第三个参数:

因为Inherited = false,所以运行结果为;

C# 特性AttributeUsage简介与使用教程

如果把Inherited = false 改为 Inherited = true,效果如下:

C# 特性AttributeUsage简介与使用教程

来源:https://www.cnblogs.com/wuguoqiang/p/15214208.html

标签:C#,AttributeUsage
0
投稿

猜你喜欢

  • Java面向对象编程的三大特征

    2023-09-19 06:20:34
  • Kotlin 编程三分钟入门

    2021-06-27 13:22:35
  • Android 通过onDraw实现在View中绘图操作的示例

    2023-07-14 02:15:38
  • Java Spring详解如何配置数据源注解开发以及整合Junit

    2021-10-31 11:03:25
  • springboot openfeign从JSON文件读取数据问题

    2023-11-09 15:55:55
  • Spring学习教程之AOP模块的概述

    2022-06-04 00:17:13
  • java实现对Hadoop的操作

    2021-10-05 16:30:37
  • Spring Security登录表单配置示例详解

    2023-10-12 09:03:55
  • Java为什么基本数据类型不需要进行创建对象?

    2022-03-16 08:59:03
  • java中申请不定长度数组ArrayList的方法

    2023-02-24 17:37:37
  • C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信

    2021-10-21 09:08:21
  • C#关联自定义文件类型到应用程序并实现自动导入功能

    2023-06-22 20:11:11
  • C# Winform消息通知系统托盘气泡提示框ToolTip控件

    2023-01-13 23:31:02
  • Android 消息机制以及handler的内存泄露

    2023-08-01 07:59:44
  • Java多线程之Semaphore实现信号灯

    2023-09-19 23:04:51
  • Spring Cache抽象-使用SpEL表达式解析

    2023-08-23 11:46:44
  • eclipse下搭建hibernate5.0环境的步骤(图文)

    2022-09-26 02:48:57
  • 基于spring 方法级缓存的多种实现

    2022-12-14 18:31:38
  • Java汉字转拼音工具类完整代码实例

    2021-07-09 21:32:18
  • Android自定义ViewGroup实现弹性滑动效果

    2022-07-25 19:44:08
  • asp之家 软件编程 m.aspxhome.com