Skip to content

Instantly share code, notes, and snippets.

@ScottIsAFool
Created April 3, 2014 09:29
Show Gist options
  • Save ScottIsAFool/9951331 to your computer and use it in GitHub Desktop.
Save ScottIsAFool/9951331 to your computer and use it in GitHub Desktop.
Get Attribute Extension
public static class AttributeExtensions
{
public static string GetDescription(this object en)
{
var type = en.GetType();
var memInfo = type.GetTypeInfo().DeclaredMembers;
var attrs = memInfo.FirstOrDefault(x => x.Name == en.ToString());
if (attrs != null)
{
var attribute = attrs.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(Description));
if (attribute != null)
{
return attribute.ConstructorArguments[0].Value.ToString();
}
}
return en.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment