カスタム属性を作成し、AttributeUsage
(または属性クラスの他の属性を)設定して、自分の属性をプライベートメソッドでのみ使用できるようにしたいのですが、それは可能ですか?
回答ありがとうございます!
カスタム属性を作成し、AttributeUsage
(または属性クラスの他の属性を)設定して、自分の属性をプライベートメソッドでのみ使用できるようにしたいのですが、それは可能ですか?
回答ありがとうございます!
メンバーのアクセシビリティに基づいて使用C# (as of 4.0)
を制限できるような機能はありません。attribute
問題は、なぜそれをしたいのかということです。
以下の属性があるため、
[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));