この質問は、前の質問で提起されたものに対処していますが、私はそれを見つけることができませんでした。基底型のリストを継承型のリストに変換する
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;
}
これは私があなたに与えた答えと同じです... –
そして、それはあなたがもともと尋ねたものに対する技術的な答えです! :) –