Created
June 10, 2010 21:11
-
-
Save idavis/433641 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.