0

Is there any way to specify the return type for PexChoose at runtime? For example PexChoose.Value(name, Type)?

This would be useful to make general models that generate values of different types depending on runtime contraints.

Marius P
  • 1
  • 1

1 Answers1

0

You could build your own helper class which will call the generic version via reflection.

For instance, to create a non-generic version of PexChoose.Value(string name)

public static class MyPexChoose
{
    public static object Value(Type myType, string name)
    {
        // Find the PexChoose.Value() method which has a single string parameter
        MethodInfo method = typeof(PexChoose).GetMethod("Value", new Type[1] {typeof(string)});
        // Make and invoke the generic version of it
        MethodInfo generic = method.MakeGenericMethod(myType);
        return generic.Invoke(typeof(PexChoose), new object[1] { name });
    }        
}

Then the call

MyPexChoose(typeof(DateTime), "MyChosen");

is equivalent to

PexChoose<DateTime>("MyChosen");
shamp00
  • 11,106
  • 4
  • 38
  • 81