如何创建表达式树来表示';String.Contains(“term”)';在C#中?
我刚刚开始使用表达式树,所以我希望这是有意义的。我正在尝试创建表达式树来表示:如何创建表达式树来表示';String.Contains(“term”)';在C#中?,c#,.net,lambda,expression-trees,C#,.net,Lambda,Expression Trees,我刚刚开始使用表达式树,所以我希望这是有意义的。我正在尝试创建表达式树来表示: t => t.SomeProperty.Contains("stringValue"); 到目前为止,我得到了: private static Expression.Lambda<Func<string, bool>> GetContainsExpression<T>(string propertyName, string propertyValue) {
t => t.SomeProperty.Contains("stringValue");
到目前为止,我得到了:
private static Expression.Lambda<Func<string, bool>> GetContainsExpression<T>(string propertyName, string propertyValue)
{
var parameterExp = Expression.Parameter(typeof(T), "type");
var propertyExp = Expression.Property(parameter, propertyName);
var containsMethodExp = Expression.*SomeMemberReferenceFunction*("Contains", propertyExp) //this is where I got lost, obviously :)
...
return Expression.Lambda<Func<string, bool>>(containsMethodExp, parameterExp); //then something like this
}
private static Expression.Lambda GetContainsExpression(string propertyName,string propertyValue)
{
var parameterExp=表达式参数(typeof(T),“type”);
var propertyExp=Expression.Property(参数,propertyName);
var containsMethodExp=Expression.*SomeMemberReferenceFunction*(“Contains”,propertyExp)//显然,这就是我迷路的地方:)
...
返回表达式.Lambda(containsMethodex,parameterExp);//然后类似于
}
我只是不知道如何引用String.Contains()方法
谢谢你的帮助 类似于:
class Foo
{
public string Bar { get; set; }
}
static void Main()
{
var lambda = GetExpression<Foo>("Bar", "abc");
Foo foo = new Foo { Bar = "aabca" };
bool test = lambda.Compile()(foo);
}
static Expression<Func<T, bool>> GetExpression<T>(string propertyName, string propertyValue)
{
var parameterExp = Expression.Parameter(typeof(T), "type");
var propertyExp = Expression.Property(parameterExp, propertyName);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var someValue = Expression.Constant(propertyValue, typeof(string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
}
class-Foo
{
公共字符串条{get;set;}
}
静态void Main()
{
var lambda=GetExpression(“条形图”、“abc”);
Foo Foo=新Foo{Bar=“aabca”};
bool test=lambda.Compile()(foo);
}
静态表达式GetExpression(string propertyName、string propertyValue)
{
var parameterExp=表达式参数(typeof(T),“type”);
var propertyExp=Expression.Property(parameterExp,propertyName);
MethodInfo method=typeof(string).GetMethod(“Contains”,new[]{typeof(string)});
var someValue=表达式.常量(propertyValue,typeof(字符串));
var containsMethodExp=Expression.Call(propertyExp,method,someValue);
返回表达式.Lambda(containsMethodex,parameterExp);
}
你可能会觉得很有帮助。这个怎么样:
Expression<Func<string, string, bool>> expFunc = (name, value) => name.Contains(value);
要执行类似以下内容的搜索:
ef.Entities.Where(entity => arr.Contains(entity.Name)).ToArray();
跟踪字符串将是:
SELECT .... From Entities ... Where Name In ("abc", "def", "qaz")
我使用我在下面创建的方法:
ef.Entities.Where(ContainsPredicate<Entity, string>(arr, "Name")).ToArray();
public Expression<Func<TEntity, bool>> ContainsPredicate<TEntity, T>(T[] arr, string fieldname) where TEntity : class {
ParameterExpression entity = Expression.Parameter(typeof(TEntity), "entity");
MemberExpression member = Expression.Property(entity, fieldname);
var containsMethods = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public)
.Where(m => m.Name == "Contains");
MethodInfo method = null;
foreach (var m in containsMethods) {
if (m.GetParameters().Count() == 2) {
method = m;
break;
}
}
method = method.MakeGenericMethod(member.Type);
var exprContains = Expression.Call(method, new Expression[] { Expression.Constant(arr), member });
return Expression.Lambda<Func<TEntity, bool>>(exprContains, entity);
}
ef.Entities.Where(包含预测(arr,“Name”)).ToArray();
公共表达式包含预测(T[]arr,字符串字段名),其中tenty:class{
ParameterExpression实体=表达式参数(typeof(tenty),“实体”);
MemberExpression成员=Expression.Property(实体、字段名);
var containsMethods=typeof(可枚举).GetMethods(BindingFlags.Static | BindingFlags.Public)
其中(m=>m.Name==“包含”);
MethodInfo method=null;
foreach(容器方法中的var m){
如果(m.GetParameters().Count()==2){
方法=m;
打破
}
}
method=method.MakeGenericMethod(member.Type);
var exprContains=Expression.Call(方法,新表达式[]{Expression.Constant(arr),member});
返回表达式.Lambda(表达式内容,实体);
}
以下是如何创建string.Contains的表达式树
var method = typeof(Enumerable)
.GetRuntimeMethods()
.Single(m => m.Name == nameof(Enumerable.Contains) && m.GetParameters().Length == 2);
var containsMethod = method.MakeGenericMethod(typeof(string));
var doesContain = Expression
.Call(containsMethod, Expression.Constant(criteria.ToArray()),
Expression.Property(p, "MyParam"));
在处的实际使用情况如果我想调用不使用“substring”@AhmD Expression启动该怎么办?不要围绕表达式。调用,并将GetMethod更改为startWith?如果您使用此代码向ORM(如linq2db)提供表达式,则需要将propertyValue提升到类中,以将其转换为SQL参数。否则,如果用户从UI提供值,则可能存在SQL注入漏洞。类似于
var someValue=Expression.Property(Expression.Constant(new{Value=propertyValue}),“Value”)
@Steve任何基于表达式的ORM,如果不能正确地转义或参数化常量表达式
,都将彻底崩溃。这本身不会有SQL注入漏洞的风险。如果你使用的工具坏得令人绝望,那么你已经有了。。。任何东西你有一个实际的例子吗?这听起来像是“FUD”。@Steve但你有没有一个例子说明它实际上并没有逃逸。不使用参数可能是有效的。。。。如果orm正确转义输入。就个人而言,为了简单起见,我会使用参数,但这是一种设计选择。
var method = typeof(Enumerable)
.GetRuntimeMethods()
.Single(m => m.Name == nameof(Enumerable.Contains) && m.GetParameters().Length == 2);
var containsMethod = method.MakeGenericMethod(typeof(string));
var doesContain = Expression
.Call(containsMethod, Expression.Constant(criteria.ToArray()),
Expression.Property(p, "MyParam"));