C# - Creating a EnumRandomizer Class
I'm stumped as about how to do this correctly. I found this and am not sure as to whether or not this could be a duplicate.
I'll start from the beginning. I was able to implement the suggestion given in the开发者_运维技巧 thread I posted by creating a class specifically for randomizing that enum, which looked like the following:
public class Foo
{
public static Random rand = new Random();
public static FooEnum[] values;
public static FooEnum GetRandomFoo()
{
values = (FooEnum[]) Enum.GetValues(typeof(FooEnum));
return values[rand.Next(0, Values.Length)];
}
public enum FooEnum { A, B, C}
}
This worked quite well. The only problem was that it obviously resulted in me wrapping a class over my enum and therefore always required me to come up with two different ways to say the same thing (e.g. class Gender wraps over enum Sex; class Weapon wraps over enum WeaponType, etc.);
This was also inefficient because it would require me to write code which had to be reimplemented over and over for the same thing. Therefore, the obvious solution to this is to create a Random Enum class which can take any kind of enum type and randomize it using the following method.
Does anyone have any ideas on how to do this? My method isn't quite working:
public class EnumRandomizer
{
private Random rand;
private Type[] values;
private int randomizerId;
private static int randomizerCount = 0;
public EnumRandomizer ()
{
rand = new Random();
randomizerId = randomizerCount;
randomizerCount++;
}
public Type RandomEnum(Type type)
{
values = (Type[]) Enum.GetValues(type);
return values[rand.Next(0, values.Length)];
}
}
Any takers?
public class EnumRandomizer
{
public static Random rand = new Random();
public static T GetRandomValue<T>()
{
T[] values = (T[])(Enum.GetValues(typeof(T)));
return values[rand.Next(0, values.Length)];
}
}
Usage:
public class Main
{
public enum FooEnum { A, B, C }
public static void Main(string[] args)
{
// Note that since the method does not take an argument which
// specifies the generic type, you must provide it explicitly.
FooEnum randomFoo = EnumRandomizer.GetRandomValue<FooEnum>();
}
}
As far as why your original isn't working: What is actually resolving is the System.Type
class, not the type you pass in. Generics are what let you dynamically assign a type in the manner you are attempting. That's why they are required for type-safe operation.
Make generic method
public static T GetRandomFoo<T>()
{
Random rand = new Random();
T[] values = (T[])Enum.GetValues(typeof(T));
return values[rand.Next(0, values.Count)];
}
Or this is correct version of you class which is not recommended to use, because it causes boxing/unboxing
public class EnumRandomizer
{
private Random rand;
private IList values;
private int randomizerId;
private static int randomizerCount = 0;
public EnumRandomizer()
{
rand = new Random();
randomizerId = randomizerCount;
randomizerCount++;
}
public object RandomEnum(Type type)
{
values = Enum.GetValues(type);
return values[rand.Next(0, values.Count)];
}
}
you can use it like this
FooEnum f = (FooEnum)new EnumRandomizer().RandomEnum(typeof(FooEnum));
Hope this helps
精彩评论