diff --git a/ClosedXML/AttributeExtensions.cs b/ClosedXML/AttributeExtensions.cs new file mode 100644 index 0000000..89b8b7f --- /dev/null +++ b/ClosedXML/AttributeExtensions.cs @@ -0,0 +1,36 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; + +namespace ClosedXML +{ + public static class AttributeExtensions + { + public static TAttribute[] GetAttributes( + this MemberInfo member) + where TAttribute : Attribute + { + var attributes = member.GetCustomAttributes(typeof(TAttribute), true); + + return (TAttribute[])attributes; + } + + public static MethodInfo GetMethod(this T instance, Expression> methodSelector) + { + return ((MethodCallExpression)methodSelector.Body).Method; + } + + public static MethodInfo GetMethod(this T instance, Expression> methodSelector) + { + return ((MethodCallExpression)methodSelector.Body).Method; + } + + public static bool HasAttribute( + this MemberInfo member) + where TAttribute : Attribute + { + return GetAttributes(member).Any(); + } + } +} diff --git a/ClosedXML/Attributes/ColumnOrderAttribute.cs b/ClosedXML/Attributes/ColumnOrderAttribute.cs deleted file mode 100644 index b85c541..0000000 --- a/ClosedXML/Attributes/ColumnOrderAttribute.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace ClosedXML.Attributes -{ - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] - public class ColumnOrderAttribute : Attribute - { - public ColumnOrderAttribute(long order) - { - this.Order = order; - } - - public long Order { get; private set; } - } -} diff --git a/ClosedXML/Attributes/XLColumnAttribute.cs b/ClosedXML/Attributes/XLColumnAttribute.cs new file mode 100644 index 0000000..0d33214 --- /dev/null +++ b/ClosedXML/Attributes/XLColumnAttribute.cs @@ -0,0 +1,43 @@ +using System; +using System.Linq; +using System.Reflection; +using ClosedXML; +using ClosedXML.Excel; + +namespace ClosedXML.Attributes +{ + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] + public class XLColumnAttribute : Attribute + { + public String Header { get; set; } + public Boolean Ignore { get; set; } + public Int32 Order { get; set; } + + private static XLColumnAttribute GetXLColumnAttribute(MemberInfo mi) + { + if (!mi.HasAttribute()) return null; + return mi.GetAttributes().First(); + } + + internal static String GetHeader(MemberInfo mi) + { + var attribute = GetXLColumnAttribute(mi); + if (attribute == null) return null; + return XLHelper.IsNullOrWhiteSpace(attribute.Header) ? null : attribute.Header; + } + + internal static Int32 GetOrder(MemberInfo mi) + { + var attribute = GetXLColumnAttribute(mi); + if (attribute == null) return Int32.MaxValue; + return attribute.Order; + } + + internal static Boolean IgnoreMember(MemberInfo mi) + { + var attribute = GetXLColumnAttribute(mi); + if (attribute == null) return false; + return attribute.Ignore; + } + } +} diff --git a/ClosedXML/ClosedXML.csproj b/ClosedXML/ClosedXML.csproj index d4073e5..9bdfd6d 100644 --- a/ClosedXML/ClosedXML.csproj +++ b/ClosedXML/ClosedXML.csproj @@ -51,7 +51,6 @@ - @@ -61,7 +60,8 @@ - + + @@ -329,7 +329,6 @@ -