1
我想創建一個簡單的腳本引擎,以在我的程序中的某些不可預知的情況下使用它。如何運行內存中創建的DLL並使用C#和反射從它檢索值
我可以在內存EXE文件中運行,但是我不知道如何運行內存DLL。 這裏是我的發動機(從vsj.co.uk得到它):
CSharpCodeProvider prov = new CSharpCodeProvider();
ICodeCompiler compiler = prov.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
cp.ReferencedAssemblies.Add("system.dll");
cp.ReferencedAssemblies.Add("system.xml.dll");
cp.ReferencedAssemblies.Add("system.data.dll");
cp.ReferencedAssemblies.Add("system.windows.forms.dll");
CompilerResults cr;
cr = compiler.CompileAssemblyFromSource(cp, File.ReadAllText(@"c:\test\sc2.csx"));
Assembly a = cr.CompiledAssembly;
try {
object o = a.CreateInstance(
"CSharpScript");
MethodInfo mi = a.EntryPoint;
mi.Invoke(o, null);
}
catch(Exception ex) {
MessageBox.Show(ex.Message);
}
}
,這裏是我的簡單的DLL,我想在運行時從中檢索值:
//sc2.csx
using System;
using System.Collections.Generic;
using System.Text;
namespace dynamic_scripting
{
public class DynScripting
{
public static int executeScript(string script)
{
return 1;
}
}
}
感謝提醒,請你給我關於反映一個很好的參考?關於 – 2011-03-05 11:01:10
@austin MSDN?說實話,它並不是非常複雜。當你反對泛型時,它開始變得粗糙。 – 2011-03-05 11:02:33