|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Utilities;
using Signum.Utilities.Reflection;
using Signum.Entities.Reflection;
namespace Signum.Entities
{
[Serializable]
public class EnumProxy<T> : IdentifiableEntity, IEquatable<EnumProxy<T>>
where T: struct
{
public EnumProxy()
{
selfModified = false;
}
public EnumProxy(string nombre)
{
this.toStr = nombre;
}
public static EnumProxy<T> FromEnum(T t)
{
return new EnumProxy<T>()
{
id = Convert.ToInt32(t),
};
}
public T ToEnum()
{
return (T)Enum.ToObject(typeof(T), Id);
}
public override string ToString()
{
return EnumExtensions.NiceToString(ToEnum()) ?? toStr;
}
public override bool IsNew
{
get { return false; }
}
public bool Equals(EnumProxy<T> other)
{
return EqualityComparer<T>.Default.Equals(ToEnum(), other.ToEnum());
}
public static implicit operator EnumProxy<T>(T enumerable)
{
return FromEnum(enumerable);
}
public static explicit operator T(EnumProxy<T> enumProxy)
{
return enumProxy.ToEnum();
}
}
/// <summary>
/// Equivalente a System.Nullable sin parametros de tipo: algunas helpers dinámicos
/// </summary>
public static class EnumProxy
{
public static IdentifiableEntity FromEnum(Enum value)
{
if(value == null) return null;
IdentifiableEntity ident = (IdentifiableEntity)Activator.CreateInstance(Reflector.GenerateEnumProxy(value.GetType()));
ident.Id = Convert.ToInt32(value);
return ident;
}
public static Enum ToEnum(IdentifiableEntity ident)
{
if (ident == null) return null;
Type enumType = Reflector.ExtractEnumProxy(ident.GetType());
return (Enum)Enum.ToObject(enumType, ident.id);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.
www.signumframework.com
I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...