Create a Generic IEnumerable<T> given a IEnumerable and the member datatypes

Posted by ilias on Stack Overflow See other posts from Stack Overflow or by ilias
Published on 2010-03-31T13:50:41Z Indexed on 2010/03/31 13:53 UTC
Read the original article Hit count: 553

Filed under:
|
|

Hi,

I get an IEnumerable which I know is a object array. I also know the datatype of the elements. Now I need to cast this to an IEnumerable<T>, where T is a supplied type. For instance

IEnumerable results = GetUsers();
IEnumerable<T> users = ConvertToTypedIEnumerable(results, typeof(User));

I now want to cast/ convert this to IEnumerable<User>. Also, I want to be able to do this for any type.

I cannot use IEnumerable.Cast<>, because for that I have to know the type to cast it to at compile time, which I don't have. I get the type and the IEnumerable at runtime.

- Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET