Mega Code Archive

 
Categories / C# / Reflection
 

Determines whether any custom attributes are applied to a member of a type

using System;     using System.Reflection;     public class TestClass      {         [Obsolete("This method is obsolete. ")]         public void Method1()         {}     }     public class DemoClass      {         static void Main(string[] args)          {             Type clsType = typeof(TestClass);             MethodInfo mInfo = clsType.GetMethod("Method1");             bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute));             if (isDef)              {                 ObsoleteAttribute obsAttr = (ObsoleteAttribute)Attribute.GetCustomAttribute( mInfo, typeof(ObsoleteAttribute));                 if (obsAttr != null)                     Console.WriteLine("The message is: \"{0}\".",                         obsAttr.Message);             }         }     }