Function to Load Enum-Typed Properties from Database

Here's a nice library function useful when loading enum-typed properties from your database:

public static T ToEnum<T>(int typeValue) {
    return (T)Enum.ToObject(typeof(T), typeValue);
}

Published Tuesday, April 10, 2007 8:01 AM by PaulWilson
Filed under: ,

Comments

# re: Function to Load Enum-Typed Properties from Database

To further constrain the input for T, add a type constraint of where T: struct. You can't use : enum, so struct is the next best thing.

Another handy utility enum function I use quite often:

       public static T ParseEnum<T>(string value) where T: struct

       {

           return (T)Enum.Parse(typeof(T), value, true);

       }

Tuesday, April 10, 2007 10:51 AM by jayson knight

# re: Function to Load Enum-Typed Properties from Database

I've been looking at the implementation of Enum.ToObject(Type, int) (using Lut'z Reflector, of course), but can't seem to think of a reason to use it. Why not cast the int directly to it's enum?

MyEnum = (MyEnum)typeValue;

Tuesday, April 10, 2007 4:20 PM by Steven

# re: Function to Load Enum-Typed Properties from Database

Can you demo the usage?

Tuesday, April 10, 2007 4:27 PM by Christian

# re: Function to Load Enum-Typed Properties from Database

As for the usage, consider an enum named MyEnum and a variable of that enum type that you want to load from an int value, possibly one you've stored in the database -- just do the following:

MyEnum test = ToEnum<MyEnum>(value);

As for the direct cast, that's a good question, and I could have sworn I'd tried that many times and it didn't work -- although it did just now in my test.  Maybe I'm thinking of a limitation I encountered in .net v1 that I've been working around -- but maybe I'm wrong there as I didn't retest that assumption.  Oh well, it seems that its not necessary for .net v2 at any rate, and if you have generics then you have .net v2, so mute point I suppose.

Thanks for the comments.

Tuesday, April 10, 2007 4:37 PM by PaulWilson

# re: Function to Load Enum-Typed Properties from Database

I would throw something like this into your helper:

System.Diagnostics.Debug.Assert(typeof(T).IsEnum);

Thursday, April 12, 2007 12:26 PM by Dave T

# re: Function to Load Enum-Typed Properties from Database

all these helper function can not ensure valid value of enum.

at .net 2.0,

MyEnum test = ToEnum<MyEnum>(value);

support MyEnum only have two enum value, and the value is 10, no exception will throw. So i don't know MyEnum test now have a invalid value.

Friday, April 13, 2007 2:29 AM by Didasoft

# re: Function to Load Enum-Typed Properties from Database

You can call Enum.IsDefined in the helper function first to guarantee a valid value, and if it fails then throw an exception.  The same problem occurs with the simple cast syntax, so at least using a helper function makes it easier to add extra things like this when you find it is needed.

Friday, April 13, 2007 6:02 AM by PaulWilson

# re: Function to Load Enum-Typed Properties from Database

Be careful about the performance of enum operations.

Tuesday, April 24, 2007 5:05 PM by Yang

Leave a Comment

(required) 
(required) 
(optional)
(required)