Create a generic IEnumerable <T> based on IEnumerable types and member elements

I get IEnumerable, which, as I know, is an array of objects. I also know the data type of the elements. Now I need to pass this to IEnumerable <T>, where T is the type supplied in the kit. For instance,

IEnumerable results = GetUsers();
Type t = GetType(); //Say this returns User
IEnumerable<T> users = ConvertToTypedIEnumerable(results, t);

Now I want to convert this to IEnumerable <User>. In addition, I want to be able to do this for any type.

I cannot use IEnumerable.Cast <> because for this I need to know the type that must be included at compile time, which I do not have. I get type and IEnumerable at runtime.

- thanks

+3
source share
1

Cast < > . . :

- , - . IEnumerable<T>, /IEnumerable, - , IEnumerable. , , .

+4

Source: https://habr.com/ru/post/1739310/


All Articles