How do I pass an enum in this context

故事扮演 提交于 2020-01-24 16:37:07

问题


I want to change the below function that I have written so that it will take the Enum (MyColors in this instance) as argument so that I can use the function on any suitable contiguous Enum. Everything I have tried so far has failed so I'm stuck.

    private enum MyColors { Red, Green, Blue }

    private String GetNext(String colorName)
    {
        MyColors colorValue;
        String colorNameOut = String.Empty;

        if (Enum.TryParse(colorName, out colorValue))
        {
            MyColors initial = colorValue, next = colorValue;

            for (int i = ((Int32)initial) + 1; i < 10; i++)
            {
                if (Enum.IsDefined(typeof(MyColors), i))
                {
                    next = (MyColors)i;
                    break;
                }
                else
                {
                    next = (MyColors)0;
                }
            }
            colorNameOut = next.ToString();
        }
        return colorNameOut;
    }

回答1:


The following ought to work:

private static String GetNext<T>(String colorName) where T : struct
{
     // Verify that T is actually an enum type
     if (!typeof(T).IsEnum)
        throw new ArgumentException("Type argument must be an enum type");

     T colorValue;
     String colorNameOut = String.Empty;

     if (Enum.TryParse<T>(colorName, out colorValue))
     {
        T initial = colorValue, next = colorValue;

        for (int i = (Convert.ToInt32(initial)) + 1; i < 10; i++)
        {
           if (Enum.IsDefined(typeof(T), i))
           {
              next = (T)Enum.ToObject(typeof(T), i);
              break;
           }
           else
           {
              next = (T)Enum.ToObject(typeof(T), 0);
           }
        }
        colorNameOut = next.ToString();
     }
     return colorNameOut;
}

Since the method is now generic it has to be called with a type argument of the enum type, like:

GetNext<MyColors>("Red")



回答2:


This will give you the next value in an enum or null if the input is the last value

public TEnum? GetNext<TEnum>(TEnum enm) where TEnum : struct
{
    var values = Enum.GetValues(enm.GetType());
    var index = Array.IndexOf(values, enm) + 1;
    return index < values.Length ? (TEnum?)values.GetValue(index) : null;
}

Usage

var nextColor = GetNext(MyColors.Red); 

GetValues function for silverlight

public static object[] GetValues(Type enumType)
    {
        if (!enumType.IsEnum)
        {
            throw new ArgumentException("Type '" + enumType.Name + "' is not an enum");
        }

        List<object> values = new List<object>();

        var fields = from field in enumType.GetFields()
                     where field.IsLiteral
                     select field;

        foreach (FieldInfo field in fields)
        {
            object value = field.GetValue(enumType);
            values.Add(value);
        }

        return values.ToArray();
    }


来源:https://stackoverflow.com/questions/7628022/how-do-i-pass-an-enum-in-this-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!