So really you want to parse the string into (eventually) a typed delegate.
One option, then it to parse the string into an Expression
, which you can then compile to a lambda. This is quite complex, but I have some code that will do much of this - but there may be existing parsers that already do the job in full.
As a trivial Expression
example:
var x = Expression.Parameter(typeof(double), "x");
var y = Expression.Parameter(typeof(double), "y");
var body = Expression.Multiply(x, y);
var func = Expression.Lambda<Func<double, double,double>>(body, x, y).Compile();
double z = func(123.45, 678.90);
The problem is the rest of the parsing code ;-p
I wrote a string
to Expression
parser last week, but it is more code than I'd normally post here... over 300 lines (not including recognition of bespoke external functions, or named arguments (it currently uses anonymous "?" place-holders)).
but if (as you say) interpreting is easy, then perhaps use similar code to write an Expression
that you can compile?