2016-09-14 2 views
0

을 UITestControl, 나는이 작업을 수행 할 수 있습니다캐스트 문자열을 간결하게 유지하기 위해

UIRvWindow AppWin = new UIRvWindow(); 

UITestControl path = new UITestControl(); 

path = AppWin.UnderlyingClass1.UnderLyingClass2; 

IEnumerable<WpfButton> collection = path.GetChildren().OfType<WpfButton>(); 

foreach(WpfButton button in collection) 
{ 
    System.Diagnostics.Trace.WriteLine(button.FriendlyName + " - " + button.DisplayText + " - " + button.Name + " - " + button.AutomationId); 
} 

이 잘 작동하지만이 할 수 있기를 원하는 : 기본적으로

UIRvWindow AppWin = new UIRvWindow(); 

UITestControl path = new UITestControl(); 

string x = "AppWin.UnderlyingClass1.UnderLyingClass2"; 

path = x; 

IEnumerable<WpfButton> collection = path.GetChildren().OfType<WpfButton>(); 

foreach(WpfButton button in collection) 
{ 
    System.Diagnostics.Trace.WriteLine(button.FriendlyName + " - " + button.DisplayText + " - " + button.Name + " - " + button.AutomationId); 
} 

를, 내가 가지고 문자열 목록 및 하나씩 차례로 실행하고 싶습니다. 이 일을 할 수있는 방법이 있습니까?

+0

여기에 사용 된 언어에 – pcnate

+0

태그를 지정하고 싶을 수 있습니다. 샘플 코드 게시 : 'AppWin.UnderlyingClass1.UnderLyingClass2'가 도움이 될 것입니다. 로드를 시도 했습니까? –

답변

0

반사는 동적 호출을 달성하는 방법입니다. 당신이 문자열로 유형 이름의 목록이 한 가정, 당신은 다음과 같은 수행 할 수 있습니다

List<string> classes = new List<string> { "WindowsFormsApplication1.MyClass1", "WindowsFormsApplication1.MyClass2" }; 
foreach (var typeName in classes) 
{ 
    Type type = Type.GetType(typeName); 
    var instance = Activator.CreateInstance(type); 

    var result = (type.GetMethod("GetChildren").Invoke(instance, null) as IEnumerable<object>).OfType<WpfButton>(); 

} 

모든 수업이 가상 GetChildren() 방법을 갖는 기본 클래스에서 상속하는 경우에, 당신은 조금 줄일 수 있습니다 아래 반사 : 당신이 너무 많은 종류 이상 사용하려는 경우

foreach (var typeName in classes) 
{ 
    Type type = Type.GetType(typeName); 
    var instance = Activator.CreateInstance(type); 

    IEnumerable<WpfButton> myButtons = null; 
    if(instance is MyBase) //MyBase is base class having virtual GetChildren() and this base class is derived by MyClass1, MyClass2... 
    { 
      myButtons = (instance as MyBase).GetChildren().OfType<WpfButton>(); 
    } 
} 

PS 반사 성능 문제가있을 수 있습니다,주의 및 적절한 설계 목표와 함께 사용하시기 바랍니다. 호프이 도움이 되었으면 ...

관련 문제