﻿namespace EPPlusEnumerable
{
    using System;
    using System.Reflection;

    internal static class Extensions
    {
        public static T GetCustomAttribute<T>(this MemberInfo element, bool inherit) where T : Attribute
        {
            return (T)Attribute.GetCustomAttribute(element, typeof(T), inherit);
        }

        public static T GetCustomAttribute<T>(this MemberInfo element) where T : Attribute
        {
            return (T)Attribute.GetCustomAttribute(element, typeof(T));
        }

        public static object GetValue(this PropertyInfo element, Object obj)
        {
            return element.GetValue(obj, BindingFlags.Default, null, null, null);
        }
    }
}

﻿namespace EPPlusEnumerable
{
    using System;
    using System.Reflection;

    internal static class Extensions
    {
        public static T GetCustomAttribute<T>(this MemberInfo element, Type type, bool inherit) where T : Attribute
        {
            var attr = (T)Attribute.GetCustomAttribute(element, typeof(T), inherit);
            
            if (attr == null)
            {
                return element.GetMetaAttribute<T>(type, inherit);
            }
            
            return attr;
        }

        public static T GetCustomAttribute<T>(this MemberInfo element, Type type) where T : Attribute
        {
            var attr = (T)Attribute.GetCustomAttribute(element, typeof(T));
            
            if (attr == null)
            {
                return element.GetMetaAttribute<T>(type);
            }
            
            return attr;
        }

        public static object GetValue(this PropertyInfo element, Object obj)
        {
            return element.GetValue(obj, BindingFlags.Default, null, null, null);
        }
    }
}

Update GetCustomAttribute extensions to support taking in type.
Update GetCustomAttribute extensions to support taking in type.

In cases where attributes are stored on a metadata class, .GetCustomAttribute will not locate the attributes. We need to locate the MetadataTypeAttribute on the class, find the referenced type, and search that for attributes.