Khi tạo biểu thức lambda bằng tay, tôi nhận được ngoại lệ 'Tham số không nằm trong phạm vi'.Tham số Lambda không nằm trong phạm vi - trong khi xây dựng biểu thức lambda nhị phân
Bất kỳ ý tưởng nào về những gì tôi đang làm sai?
public class OtherType
{
public string First_Name { get; set; }
public string Last_Name { get; set; }
}
static void Main(string[] args)
{
Expression<Func<OtherType, bool>> l2 =
p => p.First_Name == "Bob";
l2.Compile(); // Works
PropertyInfo property =
typeof(OtherType).GetProperty("First_Name");
ParameterExpression para =
Expression.Parameter(typeof(OtherType), "para");
ConstantExpression right =
Expression.Constant("Bob", typeof(string));
MemberExpression left =
Expression.Property(Expression.Parameter(typeof(OtherType), "para"), property);
BinaryExpression binary =
Expression.MakeBinary(ExpressionType.Equal, left, right);
Expression<Func<OtherType, bool>> l =
Expression.Lambda<Func<OtherType, bool>>(binary, new ParameterExpression[] { para });
l.Compile(); // Get a 'Lambda Parameter not in scope' exception
}
Tôi đã phải đọc lại hàng chục lần này để cuối cùng nhận ra những gì đang xảy ra. Cảm ơn Jon: D –
Ba năm sau: cảm ơn nhiều !!! :) – schglurps