IEnumerable型のプロパティを持ち、LINQを使用してパラメータを操作したいクラス(Webコントロール)があります。IEnumerableをIEnumerableに変換する<T>
IEnumerableへのリフレクションによってキャスト/変換/呼び出しする方法はありますか<T>コンパイル時に型が分からないのですか?
おそらくIEnumerable.Cast<T>
このようにそれを使用する使用するようにコードをリファクタリングしたい
Method void (IEnumerable source)
{
var enumerator = source.GetEnumerator();
if (enumerator.MoveNext())
{
var type = enumerator.Current.GetType();
Method2<type>(source); // this doesn't work! I know!
}
}
void Method2<T>(IEnumerable<T> source) {}
状況に応じて、OfTypeを使用することもできます。以下も参照してください:http://stackoverflow.com/questions/4015930/when-to-use-cast-and-oftype-in-linq –