Hi, if you want to dinamically compile the expression, you can use
something like this:
private object CreateMyMath(string sExpression)
{
StringWriter sw = new StringWriter();
// Source Code
sw.WriteLine("using System;");
sw.WriteLine("using System.Collections;");
sw.WriteLine("public class MyMath");
sw.WriteLine("{");
sw.WriteLine(" public decimal Evaluate()");
sw.WriteLine(" {");
sw.WriteLine(" return " + sExpression + ";");
sw.WriteLine(" }");
sw.WriteLine("}");
Microsoft.CSharp.CSharpCodeProvider cs = new
Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.ICodeCompiler ic = cs.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters cp = new
System.CodeDom.Compiler.CompilerParameters();
System.CodeDom.Compiler.CompilerResults Results =
ic.CompileAssemblyFromSource(cp, sw.ToString());
return Results.CompiledAssembly.CreateInstance("MyMath");
}
And use it like this:
object MyMath = CreateMyMath(sExpression);
return (decimal) MyMath.GetType().GetMethod("Evaluate").Invoke(MyMath ,
null);
-----Original Message-----
From: dotnet discussion [mailto:[EMAIL PROTECTED]] On Behalf Of
Aaron Fanetti
Sent: Sunday, April 14, 2002 12:47 AM
To: [EMAIL PROTECTED]
Subject: [DOTNET] Evaluate math expression strings?
Does the framework have (or is someone willing to share :) ) a utility
class to do this? Or will I need to build my own interpreter? Something
like...
int result = MyMath.Evaluate( "(10 / 2) * 3" );
Thanks,
Aaron
You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.