Skip to content

Instantly share code, notes, and snippets.

@pfrozi
Last active March 12, 2018 21:05
Show Gist options
  • Save pfrozi/b040f7920d9c02539b9d17586a15541e to your computer and use it in GitHub Desktop.
Save pfrozi/b040f7920d9c02539b9d17586a15541e to your computer and use it in GitHub Desktop.
Get attribute information from ENUM type
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