1

Consider this code fragment;

int sum = 0;
sum = Expression.Evaluate("1+1");

where the value of sum = 2

I do have an expression that will compute values but i want that expression to be build programatically then evaluate the result. I don't have any idea what class or namespace that I will dealing with. Anyone can help me.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
Rob
  • 638
  • 3
  • 14
  • 34
  • `I do have an expression that will compute values`. does that means the code fragment you provided is working as expected? `i want that expression to be build programatically`. can you provide an example like input and output. – Azodious Jan 25 '12 at 05:27
  • This answer is probably what you're looking for: http://stackoverflow.com/a/53852/1106671 – David Merriman Jan 25 '12 at 05:29
  • 1
    possible duplicate of [How can I evaluate a C# expression dynamically?](http://stackoverflow.com/questions/53844/how-can-i-evaluate-a-c-sharp-expression-dynamically). Voting to close as duplicate. If it is homework - mark as such with details on what you need help with, otherwise it is already covered before. – Alexei Levenkov Jan 25 '12 at 05:33

3 Answers3

1

You could use Expression Trees:

Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y.

You can compile and run code represented by expression trees.

Note: This problem can be solved using System.Reflection.Emit and work directly with MSIL, but the resulting code is hard to write and read.

After a little browsing, I suggest you checkout Flee on Codeplex: Fast Lightweight Expression Evaluator :

Flee is an expression parser and evaluator for the .NET framework. It allows you to compute the value of string expressions such as sqrt(a^2 + b^2) at runtime. It uses a custom compiler, strongly-typed expression language, and lightweight codegen to compile expressions directly to IL. This means that expression evaluation is extremely fast and efficient.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
0

You can build Expressions trees either using lambdas or by building them programatically using classes in the System.Linq.Expressions namespace.

See MSDN for more info.

m0sa
  • 10,712
  • 4
  • 44
  • 91
0

Have you seen http://ncalc.codeplex.com ?

It's extensible, fast (e.g. has its own cache) enables you to provide custom functions and varaibles at run time by handling EvaluateFunction/EvaluateParameter events. Example expressions it can parse:

Expression e = new Expression("Round(Pow(Pi, 2) + Pow([Pi2], 2) + X, 2)");

  e.Parameters["Pi2"] = new Expression("Pi * Pi");
  e.Parameters["X"] = 10;

  e.EvaluateParameter += delegate(string name, ParameterArgs args)
    {
      if (name == "Pi")
      args.Result = 3.14;
    };

  Debug.Assert(117.07 == e.Evaluate());

It also handles unicode & many data type natively. It comes with an antler file if you want to change the grammer. There is also a fork which supports MEF to load new functions.

It also supports logical operators, date/time's strings and if statements.

GreyCloud
  • 3,030
  • 5
  • 32
  • 47