0

我想让用户将一段代码作为文本编写,即时编译,在我从数据源获得的集合上执行它并获得结果。

我一直想将通用列表作为参数传递给动态编译的代码,但我想不出办法。以下是我的代码:

//User writes this code in a textbox and executes
var executeCode = @"//this line doesn't work because I don't know the type
                            MessageBox.Show(Parameters[0].Count());
                            //following works fine
                            var t = new List<string>{""asd"", ""xyz""};
                            var a = t.Select(x => x).First();
                            MessageBox.Show(a); 
                            return (object) a;";

#region template Code
executeCode = @"
                using System;
                using System.IO;
                using System.Windows.Forms;
                using System.Linq;
                using System.Collections.Generic;

                namespace MyNamespace {
                public class MyClass {

                public object DynamicCode(params object[] Parameters) {
                " + executeCode +
                "}   }    }";
#endregion template Code

var references = new[] { "System.dll", "System.Core.dll", "System.Windows.Forms.dll" };

var compilerParams = new CompilerParameters
            {
                GenerateInMemory = true,
                TreatWarningsAsErrors = false,
                GenerateExecutable = false,
                CompilerOptions = "/optimize"
            };
        compilerParams.ReferencedAssemblies.AddRange(references);

        var provider = new CSharpCodeProvider();
        var compile = provider.CompileAssemblyFromSource(compilerParams, executeCode);

        if (compile.Errors.HasErrors)
        {
            var text = compile.Errors.Cast<CompilerError>()
                              .Aggregate("Compile Error: ", (current, ce) => current + ("rn" + ce.ToString()));
            throw new Exception(text);
        }

        // execute the compiled code

        var assembly = compile.CompiledAssembly;
        var myObject = assembly.CreateInstance("MyNamespace.MyClass");
        if (myObject == null)
        {
            MessageBox.Show("Couldn't load class.");
            return;
        }

        var sampleList = new List<string> { "abcd", "bcd" };
        var codeParams = new object[] { sampleList };

        try
        {
            var loResult = myObject.GetType().InvokeMember("DynamicCode",BindingFlags.InvokeMethod, null, myObject, codeParams);
            MessageBox.Show("Method Call Result:\r\n\r\n" + loResult, "Compiler Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception loError)
        {
            MessageBox.Show(loError.Message, "Compiler Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

在上面的代码中,我只是传递了一个字符串列表。但我会用一个对象来代替它。用户将编写 Linq 查询来过滤集合,我即时编译并返回结果。

对此的任何指示都会非常有帮助。(我使用 C# 4.5)

4

1 回答 1

2

有几种选择。

  1. 将参数对象的类型更改为List<string>[]. 如果你总是知道你正在传递一个List<string>.

  2. 输入动态生成的代码:((List<string)Parameters[0]).Count;它有点笨拙,但会消除错误。

  3. 将参数对象的类型更改为dynamic. 由于您在运行时编译代码,因此编译时类型检查可能不是您的优先事项。

于 2013-08-03T00:30:18.340 回答