在C#中,把字串符當表達式看,相當js中的eval

Posted on 2007-01-31 16:16  零度海洋  阅读(271)  评论(0)    收藏  举报

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;

namespace csrepl
{
    class Program
    {
        //調用方式,最後輸出的結果要用return
        static string funcprefix = "using System;\r\n"
            + "public delegate void Proc();\r\n"
            + "public class Wrapper { \r\n"
            + "  public static object Set(string name, object value) { \r\n"
            + "    AppDomain.CurrentDomain.SetData(name, value);\r\n"
            + "    return value; \r\n"
            + "  }\r\n"
            + "  public static object Get(string name) { \r\n"
            + "    return AppDomain.CurrentDomain.GetData(name);\r\n"
            + "  }\r\n"
            + "  public static object Invoke(Proc proc) { \r\n"
            + "    proc();\r\n"
            + "    return null; \r\n"
            + "  }\r\n"
            + "  public static object Eval() { ";
        static string funcsuffix = "; \r\n} }";


        static string StringEval(string expr)
        {
            string program = funcprefix + expr + funcsuffix;

            ICodeCompiler compiler = CodeDomProvider.CreateProvider("C#").CreateCompiler();
            CompilerParameters cp = new CompilerParameters();
            cp.GenerateExecutable = false;
            cp.GenerateInMemory = true;
     

            CompilerResults results = compiler.CompileAssemblyFromSource(cp, program);
            if (results.Errors.HasErrors)
            {
                if (results.Errors[0].ErrorNumber == "CS0029")
                    return StringEval("Invoke(delegate { " + expr + "; })");
                return results.Errors[0].ErrorText;
            }
            else
            {
                Assembly assm = results.CompiledAssembly;
                Type target = assm.GetType("Wrapper");
                MethodInfo method = target.GetMethod("Eval");
                object result = method.Invoke(null, null);
                return result == null ? null : result.ToString();
            }
        }

        static void Main(string[] args)
        {

            while (true)
            {
                Console.Write("> ");
                Console.Out.Flush();
                string expr = Console.ReadLine();
                if (expr == null)
                    break;
                try
                {
                    string result = StringEval(expr);
                    Console.WriteLine(result);
                }
                catch (TargetInvocationException ex)
                {
                    Console.WriteLine(ex.InnerException.GetType().Name + ": " + ex.InnerException.Message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
                }
            }

        }
    }
}

 

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3