C# .NET - Store expression in table and eval at runtime

Asked By John on 10-Nov-11 03:35 AM
I want to store a setting (which is a C# date expression) as a string which when retrieved will be re-calculated. It is to allow date ranges to be stored and re-calculated when retrieved...

Config table
---------------------------
setting    parameter
------------   -------------
fromdate  System.DateTime.Now.AddDays(-7)+" 00:00:00"
todate    System.DateTime.Now+" 23:59:59"


So, here the previous seven days... I then want to assign the output to a DateTime variable (something like):-

DateTime myFromDate = eval(s_fromdate);

Is the only way to do this, to simulate the javascriptscript eval function (or write equivalent code in C#)?

Thanks...


Kirtan Patel replied to John on 10-Nov-11 05:39 AM
Here is how to Evaluate C# code at RunTime 

using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
public static object Eval(string sCSCode)
{
 
  CSharpCodeProvider c = new CSharpCodeProvider();
  ICodeCompiler icc = c.CreateCompiler();
  CompilerParameters cp = new CompilerParameters();
 
  cp.ReferencedAssemblies.Add("system.dll");
  cp.ReferencedAssemblies.Add("system.xml.dll");
  cp.ReferencedAssemblies.Add("system.data.dll");
  cp.ReferencedAssemblies.Add("system.windows.forms.dll");
  cp.ReferencedAssemblies.Add("system.drawing.dll");
 
  cp.CompilerOptions = "/t:library";
  cp.GenerateInMemory = true;
 
  StringBuilder sb = new StringBuilder("");
  sb.Append("using System;\n");
  sb.Append("using System.Xml;\n");
  sb.Append("using System.Data;\n");
  sb.Append("using System.Data.SqlClient;\n");
  sb.Append("using System.Windows.Forms;\n");
  sb.Append("using System.Drawing;\n");
 
  sb.Append("namespace CSCodeEvaler{ \n");
  sb.Append("public class CSCodeEvaler{ \n");
  sb.Append("public object EvalCode(){\n");
  sb.Append("return " + sCSCode + "; \n");
  sb.Append("} \n");
  sb.Append("} \n");
  sb.Append("}\n");
 
  CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());
  if (cr.Errors.Count > 0)
  {
    MessageBox.Show("ERROR: " + cr.Errors[0].ErrorText,
      "Error evaluating cs code", MessageBoxButtons.OK,
      MessageBoxIcon.Error);
    return null;
  }
 
  System.Reflection.Assembly a = cr.CompiledAssembly;
  object o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler");
 
  Type t = o.GetType();
  MethodInfo mi = t.GetMethod("EvalCode");
 
  object s = mi.Invoke(o, null);
  return s;
 
}