Tôi có một lớp (điều khiển web) có thuộc tính kiểu IEnumerable và muốn làm việc với tham số bằng LINQ.Chuyển đổi/Truyền IEnumerable thành IEnumerable <T>
Có cách nào để truyền/chuyển đổi/gọi qua phản ánh tới IEnumerable <T> không biết loại lúc biên dịch không?
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) {}
Tùy thuộc vào tình huống bạn cũng có thể sử dụng 'OfType', chỉ cần chỉ ra. Xem thêm: http://stackoverflow.com/questions/4015930/when-to-use-cast-and-oftype-in-linq –