using System; using System.Reflection; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)] class HelpAttribute : Attribute { public string field = "field"; public string Help { get; set; } public HelpAttribute() { Help = "no help"; } public HelpAttribute(string help) { this.Help = help; } } [Help] class ClassWithCustomAttribute { public ClassWithCustomAttribute() { ReadAttributes(); // Method invoked only when debugging MethodDebug(); } [Obsolete("This method should not be used anymore!", true)] public void Method0() { // do smth } [System.Diagnostics.Conditional("Debug")] public void MethodDebug() { Console.WriteLine("Method for debug!"); } protected void ReadAttributes() { // if AllowMultiple = false but inherited is true the base class Help is overridden by the Derive class Help attribute. Console.WriteLine("Assigned attributes of "+this+": "); foreach(HelpAttribute attr in this.GetType().GetCustomAttributes(true)) { Console.WriteLine("\tHelp: \t" + attr.Help); Console.WriteLine("\tField: \t"+attr.field); } } } [Help(field = "Some help field")] class DerivedClassWithCustomAttribute : ClassWithCustomAttribute { public DerivedClassWithCustomAttribute() { ReadAttributes(); } } public class Test { public static void Main() { ClassWithCustomAttribute cca = new ClassWithCustomAttribute(); // OUTPUT: // Assigned attributes: // Help: no help // Field: field DerivedClassWithCustomAttribute dca = new DerivedClassWithCustomAttribute(); // OUTPUT: // Assigned attributes of DerivedClassWithCustomAttribute: // Help: no help // Field: Some help field // In case the Inherited option of AttributeUsage applied to HelpAttribute // we would get following output when invoke ctor of DerivedClassWithCustomAttribute: // OUTPUT: // Assigned attributes of DerivedClassWithCustomAttribute: // Help: no help // Field: Some help field // Help: no help // Field: field } }