0
我想在我的遊戲中實現C#作爲腳本語言。運行時編譯C#
我的問題是,如果我想使用在遊戲核心(exe)中定義的類,我的腳本將無法編譯。
腳本是這樣的:
using System;
using ConsoleApplication1;
class Script
{
private static void Call()
{
Console.WriteLine("called");
}
public static void Init()
{
Console.WriteLine("Script");
Call();
GameObject myO; // THIS IS WHAT I WANT TO GET WORKED,
//IF THIS IS COMMENTED OUT, IT COMPILES FINE, GAMEOBJECT
// IS DEFINED IN THE "ConsoleApplication1" NAMESPACE.
}
}
該腳本的MDX樣品中編譯,如:
static void Main(string[] args)
{
CodeDomProvider provider = new CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
//cp.CompilerOptions = "/target:library";
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
cp.IncludeDebugInformation = false;
cp.ReferencedAssemblies.Add("ConsoleApplication2.exe");
cp.ReferencedAssemblies.Add("System.dll");
CompilerResults cr = provider.CompileAssemblyFromFile(cp, "script.cs");
if (!cr.Errors.HasErrors)
{
Console.WriteLine("Success");
cr.CompiledAssembly.GetType("Script").GetMethod("Init").Invoke(null, null);
}
Console.ReadLine();
}
有什麼辦法來調用功能或創建的「ConsoleApplication1」定義的對象命名空間通過腳本?
我是否錯過了你遇到什麼編譯時錯誤的部分? – Tipx 2011-05-13 17:47:50
哦,我很笨...檢查編譯器錯誤[0],現在它的工作原理,謝謝 – Blaxx 2011-05-13 18:00:57
'ConsoleApplication2.exe'中定義了'ConsoleApplication1.GameObject'嗎? – 2011-05-13 18:01:07