我使用.NET集成的C#編譯器做了這個'腳本'的事情。
這是一些工作要做,但基本上是看起來像這樣:
public Assembly Compile(string[] source, string[] references) {
CodeDomProvider provider = new CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters(references);
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
cp.TreatWarningsAsErrors = false;
try {
CompilerResults res = provider.CompileAssemblyFromSource(cp, source);
// ...
return res.Errors.Count == 0 ? res.CompiledAssembly : null;
}
catch (Exception ex) {
// ...
return null;
}
}
public object Execute(Assembly a, string className, string methodName) {
Type t = a.GetType(className);
if (t == null) throw new Exception("Type not found!");
MethodInfo method = t.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); // Get method
if (method == null) throw new Exception("Method '" + methodName + "' not found!"); // Method not found
object instance = Activator.CreateInstance(t, this);
object ret = method.Invoke(instance, null);
return ret;
}
真正的代碼做了很多更多的東西,包括代碼編輯器。
它在我們工廠多年來工作得非常好。
這樣用戶就可以使用C#作爲腳本,因此可以像使用相同的API。
您可以使用看起來像普通的.cs
文件的代碼模板。它在運行時生成並提供給Compile
的source
參數。
using System;
using System.IO;
using ...
namespace MyCompany.Stuff {
public class ScriptClass {
public object main() {
// copy user code here
// call your own methods here
}
// or copy user code here
private int test(int x) { /* ... */ }
}
}
例子:
string[] source = ??? // some code from TextBoxes, files or whatever, build with template file...
string[] references = new string[] { "A.dll", "B.dll" };
Assembly a = Compile(source, references);
object result = Execute(a, "MyCompany.Stuff.ScriptClass", "main");
來源
2013-07-18 12:45:37
joe
你嘗試過這麼遠嗎?你可以看看託管擴展性框架(http://msdn.microsoft.com/en-us/library/dd460648.aspx) –
其實我偶然發現了MEF,同時使用谷歌搜索,但是到目前爲止我沒有做任何事情這個方向,因爲我仍然在研究應用程序的更基本的東西,並認爲在開始之前向專業人士提供正確方向的提示可能是一個好主意 - 所以非常感謝,我一定會進入這個! – user871784