Created
April 3, 2014 09:29
-
-
Save ScottIsAFool/9951331 to your computer and use it in GitHub Desktop.
Get Attribute Extension
This file contains hidden or 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 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