是的,AttributeUsage 属性可以用于方法。在 C# 中,AttributeUsage 属性用于指定自定义属性应用于哪些代码元素(如类、方法、属性等)。当你想要将自定义属性应用于方法时,可以在自定义属性的定义中使用 AttributeUsage 属性,并将其 AttributeTargets 参数设置为 Method。
下面是一个示例,展示了如何将自定义属性应用于方法:
using System;
// 自定义属性
[AttributeUsage(AttributeTargets.Method)]
public class MyCustomAttribute : Attribute
{
    public string Message { get; set; }
    public MyCustomAttribute(string message)
    {
        Message = message;
    }
}
class Program
{
    // 使用自定义属性应用于方法
    [MyCustom("Hello, this is a custom attribute applied to a method!")]
    public void MyMethod()
    {
        Console.WriteLine("This is the method with the custom attribute.");
    }
    static void Main(string[] args)
    {
        MyMethod();
    }
}
在这个示例中,我们定义了一个名为 MyCustomAttribute 的自定义属性,并使用 AttributeUsage 属性将其应用于 MyMethod 方法。当调用 MyMethod 方法时,将输出自定义属性的消息。