Is it possible to call the "Select" method through reflection

I am working on a project, and the situation I came across is that I need to “select” data from the request object. Unfortunately, I cannot know what type the actual requested object is at compile time. So I tried calling the "select" method through reflection. The code I've tried so far is below.

....
.......
//suppose that I've got TSource and TResult at runtime.
Type argumentType = Model.GetArgumentType();

//get a queryable object from modle.
IEnumerable obj   = Model.GetQueryableObject(); 

//looking for Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
var selectMethod = typeof(System.Linq.Enumerable)
                  .GetMethods(BindingFlags.Static | BindingFlags.Public)
                  .Where(mi => mi.Name == "Select" &&                            
                               mi.GetParameters()[1].ParameterType.GetGenericArguments().Count() == 2)
                  .Single()
                  .MakeGenericMethod(new Type[] { argumentType, argumentType });

//that is where I have no idea how to do it
var result = selectMethod.Invoke(null, new object[] { obj, **xxxxxx** });
....
...

Can someone tell me how I can do " Func<TSource, TResult>" for selectMethod to call the "Select" method through reflection? Thank.




Update: @Jon Skeet. . , (, ). , ( ). , .

+3
1

( .)

-

Type funcType = typeof(Func<,>).MakeGenericType(argumentType, argumentType);

( , , , TSource TResult .)

Delegate.CreateDelegate funcType "select", .

+1

All Articles