none
passing enum type to generic method RRS feed

  • Question

  • say, I have this enum:

    public enum MyEnum
    { ... }

    I like to pass this enum type to a custom method with generic, is this possible?

    Dictionary<TEnum, TValue> MyMethod<TEnum, TValue>(TEnum enumType, TValue valueType)
    {
    var vs = Enum.GetValues(enumType);
    ...
    return ...
    }
    
    Dictionary<MyEnum, int> x = MyMethod(typeof(MyEnum), int);

    Monday, July 8, 2013 9:20 AM

Answers

  • Yes you can, but you might want to check that TEnum actually is enum type and you can get enum type from generic type argument instead of taking it as argument like in following sample code.

    class Program
    {
        static void Main(string[] args)
        {
            var dictionary = ToDictionary<Position>();
        }
    
        static Dictionary<TEnum, string> ToDictionary<TEnum>()
        {
            Type enumType = typeof(TEnum);
    
            if (!enumType.IsEnum)
                throw new ArgumentException("Not enum type.");
    
            var values = (TEnum[])Enum.GetValues(enumType);
    
            var dictionary = new Dictionary<TEnum, string>();
    
            foreach (var value in values)
            {
                dictionary.Add(value, Enum.GetName(enumType, value));
            }
    
            return dictionary;
        }
    }
    
    public enum Position : int
    {
        Top = 0,
        Left = 1,
        Bottom = 2,
        Right = 3
    }

    • Marked as answer by Kelmen Monday, July 15, 2013 7:05 AM
    Monday, July 8, 2013 12:15 PM

All replies

  • Yes you can, but you might want to check that TEnum actually is enum type and you can get enum type from generic type argument instead of taking it as argument like in following sample code.

    class Program
    {
        static void Main(string[] args)
        {
            var dictionary = ToDictionary<Position>();
        }
    
        static Dictionary<TEnum, string> ToDictionary<TEnum>()
        {
            Type enumType = typeof(TEnum);
    
            if (!enumType.IsEnum)
                throw new ArgumentException("Not enum type.");
    
            var values = (TEnum[])Enum.GetValues(enumType);
    
            var dictionary = new Dictionary<TEnum, string>();
    
            foreach (var value in values)
            {
                dictionary.Add(value, Enum.GetName(enumType, value));
            }
    
            return dictionary;
        }
    }
    
    public enum Position : int
    {
        Top = 0,
        Left = 1,
        Bottom = 2,
        Right = 3
    }

    • Marked as answer by Kelmen Monday, July 15, 2013 7:05 AM
    Monday, July 8, 2013 12:15 PM
  • Yes, here's how you declare and call such a method:

    public Dictionary<TEnum, TValue> MyMethod<TEnum, TValue>(TEnum enumValue, TValue typeValue)
    {
    	//...
    	return new Dictionary<TEnum, TValue>();
    }
    
    Dictionary<MyEnum, int> x = MyMethod<MyEnum, int>(MyEnum.EnumerationValue, 0);

    Monday, July 8, 2013 12:58 PM
  • Hi,

    in Dictionary  class , you can use t enum variable , but if you define a Dictionary like this:

    public  Dictionary<eNum,value> ,you shoud cast  enum variable to  int .

    Tuesday, July 9, 2013 8:06 AM