C# 表达式树:是否可以使用动态表达式方法构建?
我不熟悉表达式树,一直在尝试构建动态表达式,以便在Linq to entities查询中的C# 表达式树:是否可以使用动态表达式方法构建?,c#,.net-4.0,C#,.net 4.0,我不熟悉表达式树,一直在尝试构建动态表达式,以便在Linq to entities查询中的.Where()方法中使用。当我显式地调用Expression.Equal(exp,exp)和Expression.GreaterThan(exp,exp)等表达式方法时,我可以让这一切正常工作。我想做的是不必硬编码Expression.Method,这样我就可以传递一个带有方法名的字符串,并使用它动态构建。下面我举一个例子。这可能吗?我不知道如何继续下去 static void Main(string[
.Where()
方法中使用。当我显式地调用Expression.Equal(exp,exp)和Expression.GreaterThan(exp,exp)
等表达式方法时,我可以让这一切正常工作。我想做的是不必硬编码Expression.Method,这样我就可以传递一个带有方法名的字符串,并使用它动态构建。下面我举一个例子。这可能吗?我不知道如何继续下去
static void Main(string[] args)
{
int i = 1;
int? iNullable = 1;
object o = 1;
int j = 2;
ParameterExpression pe1 = Expression.Parameter(typeof(int));
ParameterExpression pe2 = Expression.Parameter(typeof(int));
//explicitly creating expression by calling Expression.Method works fine
Expression call = Expression.Equal(pe1, pe2);
var f = Expression.Lambda<Func<int, int, bool>>(call, pe1, pe2).Compile();
Console.WriteLine(f(i, i));
Console.WriteLine(f((int)iNullable, i));
Console.WriteLine(f(i, (int)o));
Console.WriteLine(f(i, j));
//I want to use a dynamic Expression method instead of Expression.Equal
//so that it could be substituted with Expression.GreaterThan etc.
String expressionMethod = "Equal";
//get the method
MethodInfo method = typeof(Expression)
.GetMethod(expressionMethod,
new[] { typeof(Expression), typeof(Expression) });
//I'm lost ....
Console.ReadKey();
}
static void Main(字符串[]args)
{
int i=1;
int?iNullable=1;
对象o=1;
int j=2;
ParameterExpression pe1=表达式.参数(typeof(int));
ParameterExpression pe2=表达式.参数(typeof(int));
//通过调用expression.Method显式创建表达式效果良好
表达式调用=表达式.Equal(pe1,pe2);
var f=Expression.Lambda(call,pe1,pe2.Compile();
控制台写入线(f(i,i));
控制台写入线(f((int)不可更改,i));
控制台写入线(f(i,(int)o));
控制台写入线(f(i,j));
//我想使用动态表达式方法而不是表达式。等于
//这样就可以用Expression.GreaterThan等替换它。
字符串表达式method=“Equal”;
//获取方法
MethodInfo方法=类型(表达式)
.GetMethod(表达式方法,
新[]{typeof(表达式),typeof(表达式)});
//我迷路了。。。。
Console.ReadKey();
}
原来我只需要调用这个方法!我会提出这个解决方案,以防其他人能从中受益
class Program
{
static void Main(string[] args)
{
int i = 1;
int? iNullable = 1;
object o = 1;
int j = 2;
ParameterExpression pe1 = Expression.Parameter(typeof(int));
ParameterExpression pe2 = Expression.Parameter(typeof(int));
//explicitly creating expression by calling Expression.Method works fine
Expression call = Expression.Equal(pe1, pe2);
var f = Expression.Lambda<Func<int, int, bool>>(call, pe1, pe2).Compile();
Console.WriteLine(f(i, i));
Console.WriteLine(f((int)iNullable, i));
Console.WriteLine(f(i, (int)o));
Console.WriteLine(f(i, j));
//I want to use a dynamic Expression method instead of Expression.Equal
//so that it could be substituted with Expression.GreaterThan etc.
List<String> expressionMethod = new List<string>(){"Equal", "GreaterThan"};
foreach (String s in expressionMethod) DynamicExpression(s, j, i);
Console.ReadKey();
}
static void DynamicExpression(String methodName, int n1, int n2)
{
ParameterExpression pe1 = Expression.Parameter(typeof(int));
ParameterExpression pe2 = Expression.Parameter(typeof(int));
//get the method
MethodInfo method = typeof(Expression)
.GetMethod(methodName,
new[] { typeof(Expression), typeof(Expression) });
//Invoke
Expression dynamicExp = (Expression)method.Invoke(null, new object[] { pe1, pe2 });
var f = Expression.Lambda<Func<int, int, bool>>(dynamicExp, pe1, pe2).Compile();
Console.WriteLine("Result for "
+ n1.ToString() + " "
+ methodName + " " + n2.ToString() + ": " + f(n1, n2));
}
}
类程序
{
静态void Main(字符串[]参数)
{
int i=1;
int?iNullable=1;
对象o=1;
int j=2;
ParameterExpression pe1=表达式.参数(typeof(int));
ParameterExpression pe2=表达式.参数(typeof(int));
//通过调用expression.Method显式创建表达式效果良好
表达式调用=表达式.Equal(pe1,pe2);
var f=Expression.Lambda(call,pe1,pe2.Compile();
控制台写入线(f(i,i));
控制台写入线(f((int)不可更改,i));
控制台写入线(f(i,(int)o));
控制台写入线(f(i,j));
//我想使用动态表达式方法而不是表达式。等于
//这样就可以用Expression.GreaterThan等替换它。
List expressionMethod=新列表(){“等于”、“大于”};
foreach(expressionMethod中的字符串s)DynamicExpression(s,j,i);
Console.ReadKey();
}
静态void DynamicExpression(字符串方法名,int n1,int n2)
{
ParameterExpression pe1=表达式.参数(typeof(int));
ParameterExpression pe2=表达式.参数(typeof(int));
//获取方法
MethodInfo方法=类型(表达式)
.GetMethod(方法名,
新[]{typeof(表达式),typeof(表达式)});
//援引
表达式dynamicExp=(表达式)方法.Invoke(null,新对象[]{pe1,pe2});
var f=Expression.Lambda(dynamicExp,pe1,pe2).Compile();
Console.WriteLine(“结果为”
+n1.ToString()+“”
+方法名+“”+n2.ToString()+”:“+f(n1,n2));
}
}
好的,您手头有methodinfo。“你要调用它吗?”埃里克利珀特谢谢你的轻推,这是漫长的一天。