title: Is it possible to compile and execute new code at runtime in .NET?
body: <p><strong>Note: Mathematical expression evaluation is not the focus of this question.  I want to compile and execute new code at runtime in .NET.</strong>  That being said...</p>
  
  <p>I would like to allow the user to enter any equation, like the following, into a text box:</p>
  
  <pre><code>x = x / 2 * 0.07914
  x = x^2 / 5
  </code></pre>
  
  <p>And have that equation applied to incoming data points.  The incoming data points are represented by <strong>x</strong> and each data point is processed by the user-specified equation.  I did this years ago, but I didn't like the solution because it required parsing the text of the equation for every calculation:</p>
  
  <pre><code>float ApplyEquation (string equation, float dataPoint)
  {
      // parse the equation string and figure out how to do the math
      // lots of messy code here...
  }
  </code></pre>
  
  <p>When you're processing boatloads of data points, this introduces quite a bit of overhead.  I would like to be able to translate the equation into a function, on the fly, so that it only has to be parsed once.  It would look something like this:</p>
  
  <pre><code>FunctionPointer foo = ConvertEquationToCode(equation);
  ....
  x = foo(x);  // I could then apply the equation to my incoming data like this
  </code></pre>
  
  <p>Function ConvertEquationToCode would parse the equation and return a pointer to a function that applies the appropriate math.</p>
  
  <p>The app would basically be writing new code at run time.  Is this possible with .NET?</p>