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.
....
.......
Type argumentType = Model.GetArgumentType();
IEnumerable obj = Model.GetQueryableObject();
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 });
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. . , (, ). , ( ). , .