开发者

How do I pass an enum in this context

开发者 https://www.devze.com 2023-04-10 07:10 出处:网络
I want to change the below function that I have written so that it will take th开发者_高级运维e Enum (MyColors in this instance) as argument so that I can use the function on any suitable contiguous E

I want to change the below function that I have written so that it will take th开发者_高级运维e 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;
    }


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")


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();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消