Skip to content

Instantly share code, notes, and snippets.

@idavis
Created June 10, 2010 21:11
Show Gist options
  • Save idavis/433641 to your computer and use it in GitHub Desktop.
Save idavis/433641 to your computer and use it in GitHub Desktop.
public enum TestEnum
{
None =0,
One = 1,
Two = 2
}
var zero = Enum<TestEnum>.GetValue<int>( TestEnum.None );
var one = Enum<TestEnum>.GetValue<int>( TestEnum.One );
var two = Enum<TestEnum>.GetValue<int>( TestEnum.Two );
var nine = Enum<TestEnum>.GetValue<int>( TestEnum.One | TestEnum.Eight );
#region Using Directives
using System;
using System.ComponentModel;
#endregion
namespace Corp.MeterDataSync
{
public static class Enum<T>
{
private static readonly Type _baseType = typeof (T);
public static T GetValue<T>( object value )
{
var converter = new EnumConverter( _baseType );
return (T) converter.ConvertFrom( value.ToString() );
}
}
}
@chillitom
Copy link

Cool, hadn't seen that one before, only came to C# about six months ago so my knowledge of the core is a little patchy.

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