Skip to content

Instantly share code, notes, and snippets.

@idavis
Created June 10, 2010 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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, what does EnumConverter look like? This is how my EnumUtils collection looks so far: http://gist.github.com/434306

@idavis
Copy link
Author

idavis commented Jun 11, 2010

It is the System.ComponentModel.EnumConverter class.

@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