2011-10-03 5 views
0

런타임시 PexChoose의 반환 형식을 지정할 방법이 있습니까? 예를 들어 PexChoose.Value (name, Type)?Pex 비 일반 메서드

이것은 런타임 제한 조건에 따라 다른 유형의 값을 생성하는 일반 모델을 만드는 데 유용합니다.

답변

0

리플렉션을 통해 일반 버전을 호출하는 고유 한 도우미 클래스를 만들 수 있습니다.

는 예를 들어, 그런 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 }); 
    }   
} 

호의 비 제네릭 만들

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

PexChoose<DateTime>("MyChosen"); 
동등