0

I had a requirement where user can create and store conditions in database

for eg. if(FB > 5000) then 100 else 200

it is working fine user can create the formula but my main main problem is executing that formula

so at runtime FB gets replaced with specific value and proper string is generated

i.e. int a; if(6000 > 5000) {a= 100 }else {a= 200}

To execute the above method at runtime i have used CSharpCodeProvider

                CSharpCodeProvider c = new CSharpCodeProvider();
                ICodeCompiler icc = c.CreateCompiler();
                CompilerParameters cp = new CompilerParameters();

                cp.ReferencedAssemblies.Add("system.dll");
                cp.CompilerOptions = "/t:library";
                //cp.CompilerOptions = "optimize"; 
                cp.GenerateInMemory = true;

                StringBuilder sb = new StringBuilder("");

                sb.Append("using System;\n");

                sb.Append("namespace CSCodeEvaler{ \n");
                sb.Append("public class CSCodeEvaler{ \n");
                sb.Append("public double EvalCode(){\n");
                sb.Append(condition);
                sb.Append("return cond1; \n");
                sb.Append("} \n");
                sb.Append("} \n");
                sb.Append("}\n");

                CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());
                if (cr.Errors.Count > 0)
                {
                    //Log Errors
                    return 0;
                }

                System.Reflection.Assembly assembly = cr.CompiledAssembly;
                object o = assembly.CreateInstance("CSCodeEvaler.CSCodeEvaler");

                Type t = o.GetType();
                MethodInfo mi = t.GetMethod("EvalCode");

                object s = mi.Invoke(o, null);

It is also working fine but my problem is it is taking too much time i.e performance is very slow, so is there any alternative to achieve the same ???

Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
  • There are _many_ alternatives. You're basically poorly reinventing the Business Rule Engine. The problem may be the reinventing part (are you looking for a third-party component?), or the poorly part (are you looking for a faster way for compiling code at runtime and using it?). You can also consider setting up your rules differently, the feasibility of which depends on the various inputs and outputs of your rules. Either way, as currently stated your question is either off-topic or too broad for SO. Try explaining what you have analyzed about this code and what ideas you got from that. – CodeCaster Feb 24 '15 at 12:45
  • Are you caching the generated code or are you recreating it every time? – xanatos Feb 24 '15 at 12:45
  • @xanatos it is recreating each and every time it is called only difference is the formula – Nilesh Gajare Feb 24 '15 at 12:46
  • @Nilesh Then you should cache the generated code, so that each formula is "compiled" only once – xanatos Feb 24 '15 at 12:48
  • Or, you know, not generate a method per formula-input pair, but use one formula and use input parameters. The possible improvements depend on the current usage, which OP should show. – CodeCaster Feb 24 '15 at 12:49
  • @CodeCaster i'm looking for faster way of compiling code at runtime and using it – Nilesh Gajare Feb 24 '15 at 12:49
  • @xanatos every time it is new formula with different values so how does the caching will work – Nilesh Gajare Feb 24 '15 at 12:50
  • @Nilesh How many formulas do you have? – xanatos Feb 24 '15 at 12:50
  • @xanatos there is no such limit user can add as many formula he wants – Nilesh Gajare Feb 24 '15 at 12:51
  • basically user will create master fields and use it in formula – Nilesh Gajare Feb 24 '15 at 12:51
  • @Nilesh With this logic, you need infinite hard disk space, because "user can add ... he wants". How many formulas will you have? 10? 100? 1000?10000? 100000? 1 million? – xanatos Feb 24 '15 at 12:52
  • @xanatos if i look into practical scenarios then it will not go above 20-30 – Nilesh Gajare Feb 24 '15 at 12:53
  • @Nilesh Ok... You'll have a small number of functions... That FB, why do you want to set it in text? Can't you change your `EvalCode()` to `EvalCode(int value)`? – xanatos Feb 24 '15 at 12:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71583/discussion-between-nilesh-and-xanatos). – Nilesh Gajare Feb 24 '15 at 12:56
  • @NileshGajare did you able to find way to get your code performance oriented. – Kaushal Mar 12 '20 at 13:36

0 Answers0