LinqToSql Plus Eval.Execute

Description

You can execute a C# expression at runtime using the Eval-Expression.NET library.

You can download it here: Download

The Eval-Expression.NET library can be activated with the LinqToSql Plus license.

You can specify parameter values to use in the expression from various ways:

  • Anonymous Type
  • Argument Position
  • Class Member
  • Dictionary

Under the hood, the first time an expression is executed, it's getting compiled and the delegate is stored in memory before being returned and executed. All future calls from the same expression will retrieve the delegate from memory to optimize performance.

Even with this optimization, if you have to evaluate multiple times the same expression, for example in a for loop, we highly recommend you use the delegate returned from the Compile method instead.

Execute and return a strongly typed result

You can return the result as a strongly typed type:

  • Eval.Execute<TResult>(string code)
  • Eval.Execute<TResult>(string code, object parameters)
  • Eval.Execute<TResult>(string code, params object[] parameters)

Example

// Parameter: Anonymous Type
int result = Eval.Execute<int>("X + Y", new { X = 1, Y = 2} );

// Parameter: Argument Position
int result = Eval.Execute<int>("{0} + {1}", 1, 2);

// Parameter: Class Member
dynamic expandoObject = new ExpandoObject();
expandoObject.X = 1;
expandoObject.Y = 2;
int result = Eval.Execute<int>("X + Y", expandoObject);

// Parameter: Dictionary Key
var values = new Dictionary<string, object>() { {"X", 1}, {"Y", 2} };
int result = Eval.Execute<int>("X + Y", values);

Try it online

Execute and return an object result

You can return the result as an object:

  • Eval.Execute(string code)
  • Eval.Execute(string code, object parameters)
  • Eval.Execute(string code, params object[] parameters)
// Parameter: Anonymous Type
object result = Eval.Execute("X + Y", new { X = 1, Y = 2} );

// Parameter: Argument Position
object result = Eval.Execute("{0} + {1}", 1, 2);

// Parameter: Class Member
dynamic expandoObject = new ExpandoObject();
expandoObject.X = 1;
expandoObject.Y = 2;
object result = Eval.Execute("X + Y", expandoObject);

// Parameter: Dictionary Key
var values = new Dictionary<string, object>() { {"X", 1}, {"Y", 2} };
object result = Eval.Execute("X + Y", values);

Try it online




Prime Library