jake wrote:
Is there a way to evaluate a string expression such as "55/8" to
return 6.875?
You can use the JavaScript support in .NET - the code below
will work on at least 2.0 (and you can write similar code
for 1.1) unlike the dynamic LINQ solution.
Arne
==================================================
using System;
using System.Reflection;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using Microsoft.JScript;
namespace E
{
public interface IJS
{
double Eval(string expr);
}
public class JS
{
private static IJS engine;
static JS()
{
string src = "import System; import E; public class C
implements IJS { public function Eval(expr : String) : Double { return
eval(expr); } }";
CodeDomProvider comp = new JScriptCodeProvider();
CompilerParameters param = new CompilerParameters();
param.GenerateInMemory = true;
param.ReferencedAssemblies.Add("System.dll");
param.ReferencedAssemblies.Add(System.Reflection.A ssembly.GetExecutingAssembly().Location);
CompilerResults res = comp.CompileAssemblyFromSource(param,
src);
Assembly asm = res.CompiledAssembly;
engine = (IJS)asm.CreateInstance("C");
}
public static double Eval(string expr)
{
return engine.Eval(expr);
}
}
}