, , ... , , .NET . Queryable.Where() queryableData ... , .NET , - :
MethodCallExpression call = Expression.Call(
null,
getGenericMethod<string>(typeof(Queryable), "Where", typeof(IQueryable<string>), typeof(Expression<Func<string,bool>>)),
Expression.Constant(queryableData),
Expression.Lamda(
e1,
p1)
);
IQueryable<string> res = queryableData.Provider.CreateQuery<string>(call);
getGenericMethod ( , ):
private static MethodInfo getGenericMethod<T>(Type type, string name, params Type[] paramTypes)
{
MethodInfo[] methods = type.GetMethods(name);
foreach(MethodInfo mi in methods)
{
if(!mi.IsGenericMethodDefinition)
continue;
if(mi.GetGenericArguments().Length != 1)
continue;
if(mi.GetParameters().Length != paramTypes.Length)
continue;
MethodInfo genMethod = mi.MakeGenericMethod(new Type[]{typeof(T)});
var ps = genMethod.GetParameters();
bool isGood = true;
for(int i = 0; i < ps.Length; i++)
{
if(ps[i].ParameterType != paramTypes[i])
{
isGood = false;
break;
}
}
if(isGood)
return genMethod;
}
return null;
}
, , , ...