Last active
March 12, 2018 21:05
-
-
Save pfrozi/b040f7920d9c02539b9d17586a15541e to your computer and use it in GitHub Desktop.
Get attribute information from ENUM type
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 enum ServiceErrors | |
{ | |
[ErrorDescription(10, "A data de emissão é menor que a data de movimento.", ErrorType.DadosInvalidos)] | |
DataEmissaoMenorQueMovimento, | |
[ErrorDescription(20, "Data de emissão inválida.", ErrorType.DadosInvalidos)] | |
DataEmissaoInvalida | |
} | |
public static class ServiceErrorsExtension{ | |
public static ErrorDescriptionAttribute GetErrorDescription<T>(this T serviceErrors) | |
where T : struct, IConvertible, IComparable, IFormattable | |
{ | |
return typeof(T).IsEnum?typeof(T) | |
.GetTypeInfo() | |
.GetProperty(Enum.GetName(typeof(T), serviceErrors)) | |
.GetCustomAttribute<ErrorDescriptionAttribute>():null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment