View Single Post
  #2 (permalink)  
Old October 28th, 2005, 01:11 PM
jacob jacob is offline
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found a way but I am not sure it is the easiest way or the most cost-effective way. Here is the solution I found...
Code:
using System;
using System.Reflection;

namespace Test
{
    public class MyCustomAttribute : Attribute
    {
        public string item;

        public MyCustomAttribute()
        {
            this.item = item;
        }
    }

    [MyCustom(item="myitemvalue")]
    class MyClassWithTheAttributeApplied
    {
        public static void SomeMethod()
        {
            MemberInfo info = typeof(MyClassWithTheAttributeApplied);
            object[] o = info.GetCustomAttributes(true);
            foreach(object t in o)
                Console.WriteLine(((MyCustomAttribute)t).item);
        }
    }

    class MainClass
    {
        [STAThread]
        static void Main(string[] args)
        {
            MyClassWithTheAttributeApplied.SomeMethod();
        }
    }
}
The above code will output myitemvalue. Please comment on the post if you know a better way to do this. Thanks.

Jacob.