Execute C# Scripting in a Class Library

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to execute a C# string inside a méthod ?
I would like duplicate the same behavior than DataBinder.Eval méthod.

Example:
string code = "this.myProperty * 5;";
object result = someCSharpEvaluationObject.Eval(code);

is it possible ? with reflexion pehaps ?
Thanks
 
If your class has a property myProperty you can just use

string code = this.myProperty * 5;
object result = someCSharpEvaluationObject.Eval(code);

Or did I misunderstand your question? Btw if myProperty is of type int, the
variable code should be declared as an int too.

HTH,

Razzie
 
Ph. VALLAR said:
Is it possible to execute a C# string inside a méthod ?
I would like duplicate the same behavior than DataBinder.Eval méthod.

Example:
string code = "this.myProperty * 5;";
object result = someCSharpEvaluationObject.Eval(code);

No, atleast not natively. You could write a parser and execution engine, but
that is a significant project. There *may* be a third party solution
available, but I Don't know of one.

There is a mechanism using jscript, but I don't think it will do what you
want. The jscript eval method executes in the context of the method that
calls it, which will likely not allow you to use this, etc as the method
would exist in a jscript assembly instead of in a C# one.
 
Back
Top