Home
Manage Your Code
Snippet: Get CustomAttributes of a PropertyInfo (C#)
Title: Get CustomAttributes of a PropertyInfo Language: C#
Description: Return the custom attributes of a given type on a PropertyInfo Views: 268
Author: Stephen Smith Date Added: 10/6/2011
Copy Code  
1
2        public static TAttribute[] GetCustomAttributes<TAttribute>(this PropertyInfo @this, bool inherit = true, TAttribute theDefault = null) where TAttribute : Attribute
3        {
4            if (@this == null) return null;
5
6            var result = @this.GetCustomAttributes(typeof(TAttribute), inherit);
7
8            if (result.Length == 0 && theDefault != null) return new[] { theDefault };
9
10            return (TAttribute[])result;
11        }
Usage
MyAttribute[] attributes = info.GetCustomAttributes();