Getting a list of custom attributes in .NET

Posted by Max | Posted in programming | Posted on 02-02-2010

0

Here is a simple .NET class which allows to get a list of custom attributes defined for class or property and check for their presence and access them by type


    public class AttributeList : List<Attribute>
    {
        /// <summary>
        /// Gets a list of custom attributes
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <returns></returns>
        public static AttributeList GetCustomAttributeList(ICustomAttributeProvider propertyInfo)
        {
            var result = new AttributeList();
            result.AddRange(propertyInfo.GetCustomAttributes(false).Cast<Attribute>());
            return result;
        }

        /// <summary>
        /// Finds attribute in collection by its own type or parents type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T FindAttribute<T>() where T : Attribute
        {
            return (T)Find(x => typeof(T).IsAssignableFrom(x.GetType()));
        }

        /// <summary>
        /// Finds attribute in collection by its own type or parents type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<T> FindAllAttributes<T>() where T : Attribute
        {
            return new List<T>(FindAll(x => typeof(T).IsAssignableFrom(x.GetType())).Cast<T>());
        }

        /// <summary>
        /// Finds attribute in collection by its own type or parents type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<T> FindAllAttributes<T>(Type implementsType) where T : Attribute
        {
            return new List<T>(FindAll(x => implementsType.IsAssignableFrom(x.GetType())).Cast<T>());
        }

        public bool IsAttributeSet<T>() where T : Attribute
        {
            return FindAttribute<T>() != null;
        }
    }

And here is a unit test (using MS Test framework) which shows how this helper class could be used


    [TestClass]
    public class AttributeListTest
    {
        private interface IAttr {}

        private class FirstIAttrAttribute : Attribute, IAttr {}
        private class SecondIAttrAttribute : Attribute, IAttr { }
        private class NonIAttrAttribute : Attribute { }

        [FirstIAttrAttribute]
        [SecondIAttrAttribute]
        [NonIAttrAttribute]
        private class TestClass
        {
        }

        [TestMethod]
        public void Test()
        {
            var attributeList = AttributeList.GetCustomAttributeList(typeof (TestClass));
            Assert.IsTrue(attributeList.IsAttributeSet<FirstIAttrAttribute>());
            Assert.IsFalse(attributeList.IsAttributeSet<TestClassAttribute>());
            Assert.IsInstanceOfType(attributeList.FindAttribute<SecondIAttrAttribute>(), typeof(SecondIAttrAttribute));

            // get all attributes implementing IAttr interface
            var iAttrList = attributeList.FindAllAttributes<Attribute>(typeof(IAttr));
            Assert.AreEqual(2, iAttrList.Count);

            // get all attributes derivind from Attribute class
            Assert.AreEqual(3, attributeList.FindAllAttributes<Attribute>().Count);
        }
    }

Write a comment