How can I evaluate a C# expression dynamically?

后端 未结 7 1333
情话喂你
情话喂你 2020-11-22 07:22

I would like to do the equivalent of:

object result = Eval(\"1 + 3\");
string now    = Eval(\"System.DateTime.Now().ToString()\") as string

7条回答
  •  轮回少年
    2020-11-22 08:24

    What are the performance implications of doing this?

    We use a system based on something like the above mentioned, where each C# script is compiled to an in-memory assembly and executed in a separate AppDomain. There's no caching system yet, so the scripts are recompiled every time they run. I've done some simple testing and a very simple "Hello World" script compiles in about 0.7 seconds on my machine, including loading the script from disk. 0.7 seconds is fine for a scripting system, but might be too slow for responding to user input, in that case a dedicated parser/compiler like Flee might be better.

    using System;
    public class Test
    {
        static public void DoStuff( Scripting.IJob Job)
        {
            Console.WriteLine( "Heps" );
        }
    }
    

提交回复
热议问题