我想让用户将一段代码写成文本,动态编译,在我从数据源获取的集合上执行它并获得结果。
我一直想把通用列表作为参数传递给动态编译的代码,但我无法找到方法。以下是我的代码:
//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)
答案 0 :(得分:2)
有几种选择。
将Parameters对象的类型更改为List<string>[]
。如果你总是知道你正在传递List<string>
。
投射动态生成的代码:((List<string)Parameters[0]).Count;
它有点笨重,但会消除错误。
将Parameters对象的类型更改为dynamic
。由于您是在运行时编译代码,因此编译时类型检查可能不是您的优先考虑事项。