I am trying to use Expression.Call
for a general static method. Unfortunately, there is no signature of the invocation method, which allows to give general type arguments and the info argument of the method.
Perhaps this is possible?
What I'm specifically trying to do is write a helper class that can dynamically sort IEnumerable(DataRow)
through Linq.
Unfortunately, I have to use DataRowExtensions
to get the field that I want to sort in a Lambda expression.
The original code comes from http://aonnull.blogspot.de/2010/08/dynamic-sql-like-linq-orderby-extension.html .
The (experimental) code snippet is as follows:
Type type = typeof(T);
IEnumerable<MethodInfo> extensions = GetExtensionMethods(Assembly.GetAssembly(typeof(DataRowExtensions)), typeof(DataRow));
ParameterExpression arg = Expression.Parameter(typeof(DataRow), "x");
MethodInfo mi = extensions.ToList()[0];
var methArg = Expression.Parameter(typeof(String), "\"" + orderByInfo + "\"");
MethodCallExpression expr = Expression.Call(null, mi, arg, methArg);
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), typeof(Object));
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
Runtime Expression.Call
, , , Field .