Home > Development > Reflection and Custom Attributes

Reflection and Custom Attributes


If you need to annotate your methods with custom attributes and would like to determine which classes and methods use the attribute at run-time then you need to use GetCustomAttributes() method in .NET. Below is some C# code showing how this is done.

using System;
using System.Linq;
using System.Reflection;

namespace ReflectionAttributesSample
{

    [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
    public class DescriptionAttribute : Attribute
    {
        private readonly string _description;

        public DescriptionAttribute(string description)
        {
            _description = description;
        }

        public string Description
        {
            get { return _description; }
        }
    }

    [Description("Class Name Main")]
    class Program
    {

        [Description("Instance Method Named Print")]
        public void Print(){}

        [Description("Private Static Method Named Main")]
        static void Main()
        {

            /*
             * 
             * Get attributes from a specific class / struct type of a specific attribute type
             * 
             */

            Type t = typeof (Program);
            var descriptionattributes = t.GetCustomAttributes(typeof (DescriptionAttribute), false);

            foreach (DescriptionAttribute attr in descriptionattributes)
                Console.WriteLine(attr.Description);

            /*
             * 
             * Get attributes from a specific method of a specific attribute type
             * 
             */

            foreach(DescriptionAttribute attr in 
                t.GetMethod("Main", BindingFlags.NonPublic | BindingFlags.Static).GetCustomAttributes(typeof (DescriptionAttribute), false))
                Console.WriteLine(attr.Description);

            /*
             * 
             * Get attributes from all of a specific attribute type
             * 
             */

            foreach (MethodInfo attr in t.GetMethods(BindingFlags.NonPublic | BindingFlags.Public |  
                BindingFlags.Static | BindingFlags.Instance))
            {
                var descattrs = attr.GetCustomAttributes(typeof (DescriptionAttribute), false);
                if(!descattrs.Any())
                    continue;

                Console.WriteLine("Method: {0} - {1}",attr.Name,((DescriptionAttribute)(descattrs[0])).Description);
            }


            Console.WriteLine("Press any key to quit");
            Console.ReadKey();
        }
    }
}

Blair..

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: