Last active
December 27, 2015 16:49
-
-
Save psantiago/7357499 to your computer and use it in GitHub Desktop.
Provides a few helper functions to get attributes from types and enums, and a couple specific functions to get the display name from types/instances similarly to how Html.LabelFor works.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class GetAttributeFromExtension | |
{ | |
public static T GetAttributeFromEnum<T>(this Enum instance) where T : Attribute | |
{ | |
var attrType = typeof(T); | |
var memberInfo = instance.GetType().GetMember(instance.ToString()); | |
return ((T)(memberInfo[0].GetCustomAttributes(attrType, false).FirstOrDefault())); | |
} | |
/// <summary> | |
/// Simple helper extension function to get the first attribute T on a property with name propertyName from a type. | |
/// (e.g., typeof(MyModel).GetAttribute/<DisplayAttribute/>("FirstName")) | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="type"></param> | |
/// <param name="propertyName"></param> | |
/// <param name="declaredOnlyProperties"></param> | |
/// <returns></returns> | |
public static T GetAttributeFrom<T>(this object model, string propertyName, bool declaredOnlyProperties = true) | |
where T : Attribute | |
{ | |
return GetAttribute<T>(model.GetType(), propertyName, declaredOnlyProperties); | |
} | |
/// <summary> | |
/// Simple helper extension function to get the first attribute T on a property with name propertyName from a type. | |
/// (e.g., typeof(MyModel).GetAttribute/<DisplayAttribute/>("FirstName")) | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="type"></param> | |
/// <param name="propertyName"></param> | |
/// <param name="declaredOnlyProperties"></param> | |
/// <returns></returns> | |
public static T GetAttribute<T>(this Type type, string propertyName, bool declaredOnlyProperties = true) | |
where T : Attribute | |
{ | |
var instanceProperty = declaredOnlyProperties | |
? type.GetProperty(propertyName, | |
BindingFlags.DeclaredOnly | BindingFlags.Public | | |
BindingFlags.Instance) | |
: type.GetProperty(propertyName); | |
if (instanceProperty == null) | |
{ | |
return null; | |
} | |
return ((T)(instanceProperty.GetCustomAttributes(typeof(T), false).FirstOrDefault())); | |
} | |
/// <summary> | |
/// Simple helper extension function to get the first attribute T on an enum instance. | |
/// (e.g., CasesEnum.CaseOne.GetAttribute/<DisplayAttribute/>()) | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="instance"></param> | |
/// <returns></returns> | |
public static T GetAttribute<T>(this Enum instance) where T : Attribute | |
{ | |
var attrType = typeof(T); | |
var memberInfo = instance.GetType().GetMember(instance.ToString()); | |
return ((T)(memberInfo[0].GetCustomAttributes(attrType, false).FirstOrDefault())); | |
} | |
/// <summary> | |
/// Gets all attributes for the property specified in expression. | |
/// </summary> | |
/// <typeparam name="TModel"></typeparam> | |
/// <typeparam name="TValue"></typeparam> | |
/// <param name="model"></param> | |
/// <param name="expression"></param> | |
/// <returns></returns> | |
public static IEnumerable<Attribute> GetAttributes<TModel, TValue>(this TModel model, Expression<Func<TModel, TValue>> expression) | |
{ | |
var propertyName = ((MemberExpression)expression.Body).Member.Name; | |
var instanceProperty = typeof(TModel).GetProperty(propertyName); | |
return instanceProperty.GetCustomAttributes(); | |
} | |
/// <summary> | |
/// Gets the first or default attribute of type T from the list attributes. | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="attributes"></param> | |
/// <returns></returns> | |
public static T FirstOrDefault<T>(this IEnumerable<Attribute> attributes) where T : Attribute | |
{ | |
return attributes.FirstOrDefault(i => i is T) as T; | |
} | |
/// <summary> | |
/// Works similarly to Html.LabelFor -- determines the property used in expression, and returns the display attribute's name of that property | |
/// for type TModel | |
/// (e.g., model.GetDisplayName(i => i.FirstName) ) | |
/// </summary> | |
/// <typeparam name="TModel"></typeparam> | |
/// <typeparam name="TValue"></typeparam> | |
/// <param name="model">This is only used to prevent having to explicitly define TModel and TValue. Otherwise, it's unused.</param> | |
/// <param name="expression"></param> | |
/// <returns></returns> | |
public static string GetDisplayName<TModel, TValue>(this TModel model, Expression<Func<TModel, TValue>> expression) | |
{ | |
return GetDisplayName(expression); | |
} | |
/// <summary> | |
/// Works similarly to Html.LabelFor -- determines the property used in expression, and returns the display attribute's name of that property | |
/// for type TModel | |
/// (e.g., GetDisplayName<MyModel, string>(i => i.FirstName) ) | |
/// </summary> | |
/// <typeparam name="TModel"></typeparam> | |
/// <typeparam name="TValue"></typeparam> | |
/// <param name="expression"></param> | |
/// <returns></returns> | |
public static string GetDisplayName<TModel, TValue>(Expression<Func<TModel, TValue>> expression) | |
{ | |
var propertyName = ((MemberExpression)expression.Body).Member.Name; | |
return typeof(TModel).GetAttribute<DisplayAttribute>(propertyName).Name; | |
} | |
public static string GetDisplayName(this Enum instance) | |
{ | |
return instance.GetAttribute<DisplayAttribute>().Name; | |
} | |
/// <summary> | |
/// Works similarly to Html.LabelFor -- determines the property used in expression, and returns the display attribute's name of that property | |
/// for type TModel concatenated with ':' and the result of invocaction the expression. | |
/// (e.g., model.GetDisplayAndValue(i => i.FirstName) ) | |
/// </summary> | |
/// <typeparam name="TModel"></typeparam> | |
/// <typeparam name="TValue"></typeparam> | |
/// <param name="model"></param> | |
/// <param name="ex"></param> | |
/// <returns></returns> | |
public static string GetDisplayAndValue<TModel, TValue>(this TModel model, Expression<Func<TModel, TValue>> expression) | |
{ | |
return GetDisplayName(expression) + ":" + expression.Compile().Invoke(model); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment