插件窝 干货文章 attributeusage用法详解

attributeusage用法详解

属性 attributeusage Attribute AttributeTargets 740    来源:    2025-03-18

attributeusage 是一个用于定义自定义属性(Attribute)使用方式的元数据属性。它通常用于指定自定义属性可以应用的目标类型(如类、方法、属性等),以及是否允许多次应用同一个属性等。attributeusage 是 .NET 框架中的一个特性(Attribute),用于控制自定义属性的行为。

1. AttributeUsage 的基本用法

AttributeUsage 是一个类,位于 System 命名空间下。它用于修饰自定义属性类,以指定该属性可以应用的目标类型和使用方式。

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MyCustomAttribute : Attribute
{
    // 自定义属性的实现
}

2. AttributeUsage 的参数

AttributeUsage 构造函数有三个主要参数:

  • AttributeTargets: 指定自定义属性可以应用的目标类型。它是一个枚举类型,可以组合多个值(使用按位或操作符 |)。

    [Flags]
    public enum AttributeTargets
    {
      Assembly = 1,
      Module = 2,
      Class = 4,
      Struct = 8,
      Enum = 16,
      Constructor = 32,
      Method = 64,
      Property = 128,
      Field = 256,
      Event = 512,
      Interface = 1024,
      Parameter = 2048,
      Delegate = 4096,
      ReturnValue = 8192,
      GenericParameter = 16384,
      All = 32767
    }
    

    例如,AttributeTargets.Class | AttributeTargets.Method 表示该属性可以应用于类和方法。

  • AllowMultiple: 指定是否允许在同一个目标上多次应用同一个属性。默认值为 false,表示不允许。

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    public class MyCustomAttribute : Attribute
    {
      // 自定义属性的实现
    }
    

    如果设置为 true,则可以在同一个类上多次应用该属性。

  • Inherited: 指定该属性是否可以被派生类继承。默认值为 true,表示可以继承。

    [AttributeUsage(AttributeTargets.Class, Inherited = false)]
    public class MyCustomAttribute : Attribute
    {
      // 自定义属性的实现
    }
    

    如果设置为 false,则派生类不会继承该属性。

3. 示例

以下是一个完整的示例,展示了如何使用 AttributeUsage 来定义一个自定义属性,并将其应用于类和方法。

using System;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MyCustomAttribute : Attribute
{
    public string Description { get; set; }

    public MyCustomAttribute(string description)
    {
        Description = description;
    }
}

[MyCustom("This is a class attribute")]
public class MyClass
{
    [MyCustom("This is a method attribute")]
    public void MyMethod()
    {
        Console.WriteLine("MyMethod is called.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        var classAttributes = typeof(MyClass).GetCustomAttributes(typeof(MyCustomAttribute), true);
        foreach (MyCustomAttribute attr in classAttributes)
        {
            Console.WriteLine("Class Attribute: " + attr.Description);
        }

        var methodAttributes = typeof(MyClass).GetMethod("MyMethod").GetCustomAttributes(typeof(MyCustomAttribute), true);
        foreach (MyCustomAttribute attr in methodAttributes)
        {
            Console.WriteLine("Method Attribute: " + attr.Description);
        }
    }
}

4. 输出结果

运行上述代码后,输出结果如下:

Class Attribute: This is a class attribute
Method Attribute: This is a method attribute

5. 总结

  • AttributeUsage 用于定义自定义属性的使用方式。
  • 通过 AttributeTargets 指定属性可以应用的目标类型。
  • 通过 AllowMultiple 控制是否允许多次应用同一个属性。
  • 通过 Inherited 控制属性是否可以被派生类继承。

通过合理使用 AttributeUsage,可以更灵活地控制自定义属性的行为,使其适用于不同的场景。