이 질문은 이전 질문에서 제기 된 내용을 다루지 만 확실히 찾을 수 없었습니다.기본 유형 목록을 상속 된 형식 목록으로 변환
기본 클래스의 일반 목록을 매개 변수로 사용하는 C# 클래스의 메서드가 있습니다. 상속 된 클래스의 목록을 전달해야하며이를 수행하는 방법을 정확하게 알지 못합니다. 내 시도에 오류가 있습니다. 다음은이를 보여주기위한 샘플 코드입니다.
public class A
{
public static void MethodC(List<A>)
{
// Do Something here with the list
}
}
public Class B : A
{
// B inherits from A, A is the Base Class
}
// Code utilizing the above method
List<B> listOfB = new List<B>();
A.MethodC((List<A>) listOfB); // Error: this does not work
A.MethodC(listOfB.ToList<typeof(A)>()); // Error: this does not work
A.MethodC(listOfB.ConvertAll<A>(typeof(A))); // Error: this does not work
// how can I accomplish this? It should be possible I would think
참고 : 참고로 최종 작업 방법은 다음과 같습니다. 나는 나의 문제에 대한 더 나은 해결책을 가지고있다. 그러나 기술적 인면에서 나의 질문은 부적절하게 표현 된 것이기 때문에 그 질문에 대한 답이 아니었다.
public static DataTable
ObjectCollectionToDataTable<GLIST>
(List<GLIST> ObjectCollection) where GLIST
: BaseBusinessObject
{
DataTable ret = null;
if (ObjectCollection != null)
{
foreach (var b in ObjectCollection)
{
DataTable dt = b.ToDataTable();
if (ret == null)
ret = dt.Clone();
if (dt.Rows.Count > 0)
ret.Rows.Add(dt.Rows[0].ItemArray);
}
}
return ret;
}
그건 내가 너에게 준 같은 대답이야. –
그리고 그것은 원래 당신이 물었던 것에 대한 답이기도하다. :) –