Is it possible to build a generic runtime C# expression evaluation engine?

  • Thread starter Jeffrey Ganping Chen
  • Start date
J

Jeffrey Ganping Chen

I'm trying to build a generic runtime C# expression evaluation engine to
allow the user to evaluate any C# code blocks at runtime within the
application's context. For example,

obj4.p4 = obj1.m1() + obj2.m2(p1, p2) - Class3.m3()

or

if (obj1.p1 > obj2.m2())
{
obj3.do()
}

where obj1, obj2 obj4 and Class3 are defined/instantiated in the application
domain. With the generic expression evaluation engine, the user can evaluate
any expressions allowed by the C# syntax and the methods and properties
supported by the defined classes in the application.

With C# Reflection capability, I can evaluate each operand, such as
obj1.m1(), obj2.m2(p1, p2), but I don't know how to evaluate expressions
with operators, such as +, -, *,etc which operates on two operands. I tried
to use Reflection.Emit, but I have to know the expression first to emit the
IL code, which is not possible because the user can write the expressions in
any forms.

C# debugger Watch window has the capability to allow users to input and
evaluate any supported expressions at runtime (at the breakpoints). I like
to have the similar capability but it is built into the application.

Thanks in advance for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top