Skip to content

Instantly share code, notes, and snippets.

@andre-f-paggi
Last active March 10, 2020 00:19
Show Gist options
  • Save andre-f-paggi/3b9de7fda1cbc2dee4a9e8db08212f35 to your computer and use it in GitHub Desktop.
Save andre-f-paggi/3b9de7fda1cbc2dee4a9e8db08212f35 to your computer and use it in GitHub Desktop.
Enumeration.cs -> Enum class
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
/// <summary>
/// Fonte do Código: https://github.com/HeadspringLabs/Tarantino/blob/master/src/Tarantino.Core/Commons/Model/Enumerations/Enumeration.cs
/// </summary>
public abstract class Enumeration<TVal> : IComparable where TVal : IComparable
{
protected Enumeration()
{
}
protected Enumeration(TVal value, string displayName)
{
Value = value;
DisplayName = displayName;
}
public TVal Value { get; set; }
public string DisplayName { get; set; }
public override string ToString()
{
return DisplayName;
}
public static IEnumerable<TEnum> GetAll<TEnum>() where TEnum : Enumeration<TVal>, new()
{
var type = typeof(TEnum);
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (var info in fields)
{
var instance = new TEnum();
var locatedValue = info.GetValue(instance) as TEnum;
if (locatedValue != null)
{
yield return locatedValue;
}
}
}
public static IEnumerable GetAll(Type type)
{
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (var info in fields)
{
var instance = Activator.CreateInstance(type);
yield return info.GetValue(instance);
}
}
public override bool Equals(object obj)
{
if (!(obj is Enumeration<TVal> otherValue))
return false;
var typeMatches = GetType().Equals(obj.GetType());
var valueMatches = Value.Equals(otherValue.Value);
return typeMatches && valueMatches;
}
public static bool operator ==(Enumeration<TVal> obj1, object obj2)
{
if (object.ReferenceEquals(obj1, null) && object.ReferenceEquals(obj2, null))
return true;
if (obj2 == null)
return false;
return obj2.Equals(obj1);
}
public static bool operator !=(Enumeration<TVal> obj1, object obj2)
{
if (object.ReferenceEquals(obj1, null) && object.ReferenceEquals(obj2, null))
return true;
if (obj2 == null)
return true;
return !obj2.Equals(obj1);
}
public override int GetHashCode()
=> Value.GetHashCode();
public static TEnum FromValue<TEnum>(object value) where TEnum : Enumeration<TVal>, new()
=> Parse<TEnum, TVal>((TVal)value, "value", item => item.Value.Equals(value));
public static TEnum FromValue<TEnum>(TVal value) where TEnum : Enumeration<TVal>, new()
=> Parse<TEnum, TVal>(value, "value", item => item.Value.Equals(value));
public static TEnum FromDisplayName<TEnum>(string displayName) where TEnum : Enumeration<TVal>, new()
=> Parse<TEnum, string>(displayName, "display name", item => item.DisplayName == displayName);
public virtual int CompareTo(object other)
=> Value.CompareTo(((Enumeration<TVal>)other).Value);
private static TEnum Parse<TEnum, K>(K value, string description, Func<TEnum, bool> predicate) where TEnum : Enumeration<TVal>, new()
{
var matchingItem = GetAll<TEnum>().FirstOrDefault(predicate);
if (matchingItem == null)
{
var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(TEnum));
throw new ApplicationException(message);
}
return matchingItem;
}
}
@andre-f-paggi
Copy link
Author

Enumeration usage:

public class ERegimeLancamento : Enumeration<int>, IComboBoxChaveValor
{
    public static readonly ERegimeLancamento Caixa = new ERegimeLancamento(1, "Caixa");
    public static readonly ERegimeLancamento Competencia = new ERegimeLancamento(2, "Competência");

    // Used in reflection
    public ERegimeLancamento() {}
    public ERegimeLancamento(int valor, string descricao) : base(valor, descricao) { }
}

Calling methods:
ERegimeLancamento.GetAll<ERegimeLancamento>();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment