이것은 내 코드입니다
private void FindControls<T>(Control control, List<T> list)
{
foreach (Control c in control.Controls)
{
if (c != null)
{
if (c is T)
{
list.Add(c); //<-- Problem is here
}
else
{
FindControls<T>(c, list);
}
}
}
}
나는 메시지를 받았다
" 'System.Collection.Generic.List.Add (T)'에 대한 가장 오버로드 된 일치 항목에 잘못된 인수가 있습니다."
이것은 특정 컨트롤을 찾는 또 다른 방법입니다.
private void MyMethod(Employee e)
{
List<MyUserControlType> employees = new List<MyUserControlType>();
this.FindControls<MyUserControlType>(this.MyControlRoot, employees);
foreach (var employee in employees)
{
....
}
}
Control 형식에서 상속하지 않는 MyUserControlType 형식의 컨트롤 목록을 반환하고 싶습니다. UserControl에서 상속
어떻게 해결할 수 있습니까?
다음 as
과 같은 연산자를 사용하여 캐스팅 할 수 있습니다.
if (c is T)
{
list.Add((c as T)); //<-- Problem is here
}
이 제약은 메서드 정의를 수정하여 배치 할 수 있습니다.
private void FindControls<T>(Control control, List<T> list) where T : class
{
(또는) 주석에서 @Ivan이 지적했듯이 메서드에 일반 제약 조건을 배치 할 필요가없는 캐스트 연산자를 사용하여 직접 캐스팅 할 수 있습니다.
if (c is T)
{
list.Add((T)c); //<-- Problem is here
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다