这是我的接口和枚举,稍微简化了一点。:
public interface IExpression
{
ExpressionType ExpressionType { get; }
}
public interface ILiteralExpression : IExpression
{
object Value { get; set; }
}
public interface IOperatorExpression : IExpression
{
IExpression[] Operands { get; set; }
string OperatorUniqueName { get; set; }
IOperatorExpression SetOperand(int index, IExpression expression);
}
public enum ExpressionType
{
Operator,
Literal
}
要创建一个表达式,我可以这样做:
var expression = ExpressionManager.Engines["Default"].Parser.Parse("1 + 3 * 4 + \"myVariable\"");
这相当于这样的事情:
var expression1 = ExpressionManager.CreateOperator("Add", 2).
SetOperand(0, ExpressionManager.CreateOperator("Add", 2).
SetOperand(0, ExpressionManager.CreateLiteral(1)).
SetOperand(1, ExpressionManager.CreateOperator("Multiply", 2).
SetOperand(0, ExpressionManager.CreateLiteral(3)).
SetOperand(1, ExpressionManager.CreateLiteral(4)))).
SetOperand(1, ExpressionManager.CreateLiteral("myVariable"));
我希望能够(有效地)做这样的事情:
(from e in expression
where e is ILiteralExpression && "myVariable".Equals(((ILiteralExpression)e).Value)
select (ILiteralExpression)e).ToList().
ForEach(e => e.Value = 2);
我想我需要做一些实施IQueryable
或其他事情,但我不知道从哪里开始。有什么建议么?