Skip to content

Instantly share code, notes, and snippets.

@facilita-tecnologia
Created November 24, 2017 19:45
Show Gist options
  • Save facilita-tecnologia/93edc75300ee4b08bf1939cc8ca0c6cc to your computer and use it in GitHub Desktop.
Save facilita-tecnologia/93edc75300ee4b08bf1939cc8ca0c6cc to your computer and use it in GitHub Desktop.
Conversor de Enum
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SGI
{
public static class Helper
{
// Exemplo de Utilização
// _NF_Entrada.indFinal = Helper.Generico<ConsumidorFinal>(_nfe.infNFe.ide.indFinal.Value);
public static int Generico<T>(T t)
{
int variable = -1;
variable = CastTo<int>.From(t);
return variable;
}
}
public static class CastTo<T>
{
/// <summary>
/// Conversor de Enum
/// </summary>
/// <typeparam name="S">Tipo Generico.</typeparam>
public static T From<S>(S s)
{
return Cache<S>.caster(s);
}
private static class Cache<S>
{
public static readonly Func<S, T> caster = Get();
private static Func<S, T> Get()
{
var p = Expression.Parameter(typeof(S));
var c = Expression.ConvertChecked(p, typeof(T));
return Expression.Lambda<Func<S, T>>(c, p).Compile();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment