Mic*_*y P 6 c# linq lambda expression-trees
我试图创建Expression,但失败了.
我想建立类似的东西 Expression<Func<typeof(type), bool>> expression = _ => true;
我的尝试:
private static Expression GetTrueExpression(Type type)
{
LabelTarget returnTarget = Expression.Label(typeof(bool));
ParameterExpression parameter = Expression.Parameter(type, "x");
var resultExpression =
Expression.Return(returnTarget, Expression.Constant(true), typeof(bool));
var delegateType = typeof(Func<,>).MakeGenericType(type, typeof(bool));
return Expression.Lambda(delegateType, resultExpression, parameter); ;
}
Run Code Online (Sandbox Code Playgroud)
用法:
var predicate = Expression.Lambda(GetTrueExpression(typeof(bool))).Compile();
Run Code Online (Sandbox Code Playgroud)
但是我收到了错误: Cannot jump to undefined label ''
Iva*_*oev 11
就如此容易:
private static Expression GetTrueExpression(Type type)
{
return Expression.Lambda(Expression.Constant(true), Expression.Parameter(type, "_"));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2156 次 |
| 最近记录: |