2012-11-26 3 views
3

다음과 같은 형식으로 모든 자식 컨트롤을 가져 오는 함수가 있습니다.컨트롤의 T 유형으로 자식 컨트롤 가져 오기

private IEnumerable<Control> GetControlsOfType(Control control, Type type) 
    { 
     var controls = control.Controls.Cast<Control>(); 

     return controls.SelectMany(ctrl => GetControlsOfType(ctrl, type)) 
            .Concat(controls) 
            .Where(c => c.GetType() == type); 
    } 

제네릭 형식 매개 변수를 사용하여 변환하려고합니다.

private IEnumerable<T> GetControlsOfType<T>(Control control) where T: Control 
    { 
     var controls = control.Controls.Cast<Control>(); 

     return controls.SelectMany(ctrl => GetControlsOfType<T>(ctrl)) 
            .Concat(controls) 
            .Where(c => c.GetType() == T); 
    } 

코드의 오류 번호는 입니다.

오류 6 'System.Collections.Generic.IEnumerable <T>' 'CONCAT'에 대한 정의와 가 '<TSource을 System.Linq.Queryable.Concat 최고의 확장 메서드 오버로드를 포함하지 않는다> (을 System.Linq .IQueryable <TSource>, System.Collections.Generic.IEnumerable <TSource>) '일부 무효 인수

어떻게이 문제를 해결할 수있다?

답변

3

시도 .OfType<Control>()GetControlsOfType<T>(ctrl) 후 대신이 Where

그래서 코드 읽기의 추가 :

return controls.SelectMany(ctrl => GetControlsOfType<T>(ctrl).OfType<Control>()) 
            .Concat(controls) 
            .OfType<T>(); 
+0

그는'IEnumerable을 '을 반환하고 싶어 -''는 IEnumerable이 반환을하지 않습니다? –

+0

@JonB 사실, –

+0

GetControlsOfType (ctrl)이 이미 'IEnumerable '의 유형으로 수정되었습니다. 왜'OfType ()'이 필요한가요? – ca9163d9

관련 문제