C# 自定义属性-仅为私有成员设置属性用法

C# 自定义属性-仅为私有成员设置属性用法,c#,attributes,private-members,attributeusage,C#,Attributes,Private Members,Attributeusage,我创建了一个自定义属性,我想设置属性设置(或者属性类中的其他属性),这样我的属性只能在私有方法中使用,这可能吗 提前感谢您的回答 在C#(从4.0开始)中没有这样的功能,允许您根据成员的可访问性限制属性的使用 问题是你为什么要这么做 因为下面给出的属性 [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)] sealed class MethodTestAttribute : Attri

我创建了一个自定义属性,我想设置
属性设置
(或者属性类中的其他属性),这样我的属性只能在私有方法中使用,这可能吗


提前感谢您的回答

C#(从4.0开始)
中没有这样的功能,允许您根据成员的可访问性限制
属性的使用

问题是你为什么要这么做

因为下面给出的属性

[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
sealed class MethodTestAttribute : Attribute
{
    public MethodTestAttribute()
    { }
}
下课

public class MyClass
{
    [MethodTest]
    private void PrivateMethod()
    { }

    [MethodTest]
    protected void ProtectedMethod()
    { }

    [MethodTest]
    public void PublicMethod()
    { }
}
您可以使用以下代码轻松获取私有方法的属性:

var attributes = typeof(MyClass).GetMethods().
                 Where(m => m.IsPrivate).
                 SelectMany(m => m.GetCustomAttributes(typeof(MethodTestAttribute), false));

我很想知道为什么只有私人属性。