[RESOLVED] Random Enum Generation
So I need to retrieve an enums min and max values to pass into a randomizer function for some unit testing to generate random enum values. However it needs to generate an enum member value based upon whatever random enum type is passed in.
This is what I have found so far that is close but need to call it passing the type dynamically.
Code:
public T RandomEnum<T>()
{
T[] values = (T[])Enum.GetValues(typeof(T));
return values[new Random().Next(0, values.Length)];
}
So I want to call it like this
Code:
var test = 0;
test = RandomEnum<selfProp.PropertyType>();
Re: [RESOLVED] Random Enum Generation
Figured it out using reflection and generics
Code:
Type t = selfProp.PropertyType;
MethodInfo method = GetType().GetMethod("RandomEnum").MakeGenericMethod(new Type[] { t });
var enumObject = method.Invoke(this, BindingFlags.Public | BindingFlags.Static, null, null, CultureInfo.CurrentCulture);
selfProp.SetValue(sourceObject, enumObject);