NextEnum

        public static T NextEnum<T>(this System.Random random)
    where T : struct
        {
            Type type = typeof(T);
            if (type.IsEnum == false) throw new InvalidOperationException();

            var array = Enum.GetValues(type);
            var index = random.Next(array.GetLowerBound(0), array.GetUpperBound(0) + 1);
            return (T)array.GetValue(index);
        }
Example:
public enum AjaxResultEnum
{
Other=0,
Success=1,
Fail=2
}
var value=new Random().NextEnum<AjaxResultEnum>();

Description

Generates random enumeration value

Details

  • Author: pedoc
  • Submitted on: 8/4/2015 9:41:38 AM
  • Language: C#
  • Type: T
  • Views: 880

Double click on the code to select all.

 

;