Evaluating algebra expression using an auxiliary VB class compiled on the fly and consumed in a C# project
public static class Parser


Why is this class static? When you have a class with some state, you should almost never make that state static. For example, you might be very surprised when you try using this code from multiple threads.



using (StreamReader sr = new StreamReader("template.vb"))
{
    source = sr.ReadToEnd().Replace("expression-in-x", expression);
}


You could have used File.ReadAllText() to make this simpler.



CompilerParameters cps = new CompilerParameters();
cps.GenerateExecutable = false;
cps.GenerateInMemory = true;


Using var and object initializer, you can shorten this to:

var cps = new CompilerParameters { GenerateExecutable = false, GenerateInMemory = true };


Also, cps is not a great name, don't shorten names unnecessarily.



obj = a.CreateInstance("AnyNamespace.AnyClass");
mi = obj.GetType().GetMethod("AnyMethod");


Instead of object and MethodInfo, you could store the method as a delegate:

Func&lt;double, double&gt; compiledFunction;

…

var obj = a.CreateInstance("AnyNamespace.AnyClass");
var mi = obj.GetType().GetMethod("AnyMethod");
compiledFunction =
    (Func&lt;double, double&gt;)Delegate.CreateDelegate(typeof(Func&lt;double, double&gt;), obj, mi);

…

return compiledFunction(x);


As another advantage, this is likely going to be faster, since it's not using reflection for every invocation of Evaluate().