views:

293

answers:

4

Thanks to this question I managed to work out how to constrain my generic method to accept only enums.

Now I'm trying to create a generic method so that I can bind a drop-down to any enum I choose, displaying the description in the drop-down, with the value equal to the numeric value of the enum value.

public static object EnumToDataSource<T>() where T : struct, IConvertible {
  if (!typeof(T).IsEnum) // just to be safe
    throw new Exception(string.Format("Type {0} is not an enumeration.", typeof(T)));
  var q = Enum.GetValues(typeof(T)).Cast<T>()
    .Select(x => new { ID = DataUtil.ToByte(x), Description = x.ToString() }) // ToByte() is my own method for safely converting a value without throwing exceptions
    .OrderBy(x => x.Description);
  return q;
}

Looks nice, but ToByte() always returns 0, even if my enumeration has values explicitly set, like so:

public enum TStatus : byte {
  Active = 1,
  Inactive = 0,
}

Outside the generic method, if I cast a value of type TStatus to byte, it works perfectly. Inside the generic method, if I try cast something of type T to byte I get a compiler error. I can't find anything in the Enum static interface to do this, either.

So, how do I get the numeric value of the enum inside the generic? (I'll also accept any other advice about optimizing my code gratefully...)

Edit: Um, er... turns out that the thing wasn't working... because there was a bug in my ToByte() method... (blush). Oh well, thanks anyway - I learned a lot from this!

A: 

I use the following utility function to convert an enum type to a hashtable that's bindable. It also regexes camel case names to space seperated words.

public static Hashtable BindToEnum(Type enumType)
{
    // get the names from the enumeration
    string[] names = Enum.GetNames(enumType);
    // get the values from the enumeration
    Array values = Enum.GetValues(enumType);
    // turn it into a hash table
    Hashtable ht = new Hashtable(names.Length);

    for (int i = 0; i < names.Length; i++)
        // Change Cap Case words to spaced Cap Case words
        // note the cast to integer here is important
        // otherwise we'll just get the enum string back again
        ht.Add(
            (int)values.GetValue(i),
            System.Text.RegularExpressions.Regex.Replace(names[i], "([A-Z0-9])", " $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim()
            );
    // return the dictionary to be bound to
    return ht;
}

You could easily adapt this to be a generic function by taking the top of what you put in your question and changing the definition of the function.

gjutras
+2  A: 

You can do it like this (change DataUtil.ToByte(x) to x.ToByte(null)):

public static object EnumToDataSource<T>() where T : struct, IConvertible
     {
      if (!typeof (T).IsEnum) throw new Exception(string.Format("Type {0} is not an enumeration.", typeof (T)));
      var q =
       Enum.GetValues(typeof (T)).Cast<T>().Select(x => new {ID = x.ToByte(null), Description = x.ToString()}).OrderBy(
        x => x.Description).ToArray();
      return q;
     }
Dzmitry Huba
The secret switch! Thanks! :)
Shaul
A: 

Maybe you can do something with my EnumExtensions

foreach on an enum and create a datasource.

bob
+1  A: 

I think the simplest thing to do is use the Convert class instead of a cast:

T someValueThatIsAnEnum;
byte enumValue = Convert.ToByte( (object)someValueThatIsAnEnum );

Alternatively, you can rely on the fact that enums can convert themselves to a string representation, and parse themselves back as well:

T someValueThatIsAnEnum;
string enumAsString = someValueThatIsAnEnum.ToString();
byte enunValue = (byte)Enum.Parse( typeof(T), enumAsString );
LBushkin