Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Enum into table C#

public abstract class CustomEnum
{
    private readonly string _name;
    private readonly object _id;

    protected CustomEnum( string name, object id )
    {
        _name = name;
        _id = id;
    }

    public string Name
    {
        get { return _name; }
    }

    public object Id
    {
        get { return _id; }
    }

    public override string ToString()
    {
        return _name;
    }
}

public abstract class CustomEnum<TEnumType, TIdType> : CustomEnum
    where TEnumType : CustomEnum<TEnumType, TIdType>
{
    protected CustomEnum( string name, TIdType id )
        : base( name, id )
    { }

    public new TIdType Id
    {
        get { return (TIdType)base.Id; }
    }

    public static TEnumType FromName( string name )
    {
        try
        {
            return FromDelegate( entry => entry.Name.Equals( name ) );
        }
        catch (ArgumentException ae)
        {
            throw new ArgumentException( "Illegal name for custom enum '" + typeof( TEnumType ).Name + "'", ae );
        }
    }

    public static TEnumType FromId( TIdType id )
    {
        try
        {
            return FromDelegate( entry => entry.Id.Equals( id ) );
        }
        catch (ArgumentException ae)
        {
            throw new ArgumentException( "Illegal id for custom enum '" + typeof( TEnumType ).Name + "'", ae );
        }
    }

    public static IEnumerable<TEnumType> GetAll()
    {
        var elements = new Collection<TEnumType>();
        var infoArray = typeof( TEnumType ).GetFields( BindingFlags.Public | BindingFlags.Static );

        foreach (var info in infoArray)
        {
            var type = info.GetValue( null ) as TEnumType;
            elements.Add( type );
        }

        return elements;
    }

    protected static TEnumType FromDelegate( Predicate<TEnumType> predicate )
    {
        if(predicate == null)
            throw new ArgumentNullException( "predicate" );

        foreach (var entry in GetAll())
        {
            if (predicate( entry ))
                return entry;
        }

        throw new ArgumentException( "Element not found while using predicate" );
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: player not following slide object unity 2d 
Csharp :: parsing object from text file c# 
Csharp :: C# downloadstirng download old 
Csharp :: IOS app crashing on ios 15 unity 
Csharp :: c# unzip all archive files inside directory 
Csharp :: .net core api routing not working 
Csharp :: ascii art american flag 
Csharp :: unity AppDomain 
Csharp :: if statement to check if a time is between two times c# 
Csharp :: what is vector3.one c# 
Csharp :: virtual properties and lazy loading in c# 
Csharp :: ms transform 
Csharp :: resize image and add watermark c# 
Csharp :: detect mouse in bottom of screen + unity 
Csharp :: c# date to julian YYJJJ date 
Csharp :: unity matchinfo 
Csharp :: how to pass object as test case in nunit c# 
Csharp :: in model add to give drop down message 
Csharp :: unity insert variable into string 
Csharp :: webtest fullscreen extend window maximize 
Csharp :: c# class responsible for creating instances 
Csharp :: qcombobox delegate text filter 
Csharp :: C# program applies bonus points 
Csharp :: c# result set from stored procedure 
Csharp :: BOTON PARA CAMBIAR DE VIEW ASP.NET 
Csharp :: upcasting and downcasting in c# 
Csharp :: long to binary c# 
Csharp :: add numbers c# 
Csharp :: c# Search specified string inside textbox 
Csharp :: C# listview as listbox 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =