【问题标题】:Can second parameter of Expression.Property be made dynamic?Expression.Property 的第二个参数可以动态化吗?
【发布时间】:2012-09-21 11:58:09
【问题描述】:

我正在开发一个规则引擎,我们将 xml 转换为在运行时编译的代码。为此,我们希望访问对象/集合中的属性,该属性不能作为字符串使用,而是作为表达式使用。

一个简化的例子 - 一个拥有属性 Age 的 Student 类

Expression stud = Expression.Variable(typeof(Student), "student");
Expression.Property(stud, Expression.Constant("Age"));

【问题讨论】:

  • 你知道属性的类型,或者无类型的结果就可以了吗?
  • 属性的类型可以是任何 CTS 定义的类型。为简单起见,我将 Age 属性保留为常量表达式。实际上,它将是一个派生表达式,其中包含“年龄”的值..
  • 请举例说明你的输入(一个xml?)和你想要的输出(一个表达式树),所以我们可以尝试提供一些代码......

标签: linq tree expression


【解决方案1】:

不应该是这样的吗:

// represents a variable student
var studentExpression = Expression.Variable(typeof(Student), "student");

// represents student.Age
var studenDotAgeExpression = Expression.Property(studentExpression, typeof(Student).GetProperty("Age"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2011-12-07
    • 2019-10-18
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多