IronLua源代碼下載(SVN):http://luainterface.googlecode.com/svn/trunk/
編譯:在編譯時,可能會報錯,因為缺少APP.Config文件,將項目中所有APP.Config刪除再次編譯就可以。
引用:在自己項目中引用:lua51.dll,LuaInterface.dll后可以進行后續開發。
命名空間:LuaInterface
常用方法:
C#:
注冊Lua中可調用方法:
luaVM.RegisterFunction(Lua調用方法名, 類, 類.GetMethod(C#方法名)); 注:C#不要使用方法級泛型,即 void Fun<T>(string str);,如果使用,系統自動判定T為第一個參數的類型。
加載Lua代碼
luaVM.DoString(Lua代碼);
luaVM.DoFile(Lua文件絕對路徑);
調用Lua方法
luaVM.GetFunction(Lua方法).Call(參數); 注:此處參數不要傳遞dynamic類型的類,否則Lua中無法獲取屬性值
Lua:
調用C#方法:
Say(“Ping”)
1 class ClassTestLua 2 { 3 public void Start() 4 { 5 Lua luaVM = new Lua(); 6 7 luaVM.RegisterFunction("Sleep", this, this.GetType().GetMethod("LuaSleep")); 8 luaVM.RegisterFunction("Sleep2", this, this.GetType().GetMethod("LuaSleep2")); 9 10 //載入AI文件 11 //luaVM.DoFile(System.AppDomain.CurrentDomain.BaseDirectory + _petPath + "//ai.lua"); 12 luaVM.DoString("function GetFunction(c) Sleep(c.a) end"); 13 luaVM.DoString("function GetFunction2(c) Sleep2(c.a) end"); 14 15 A<int> C = new A<int>(); 16 C.a = 10; 17 18 luaVM.GetFunction("GetFunction").Call(C);//調用lua中的GetFunction方法 19 20 try 21 { 22 23 luaVM.GetFunction("NoGetFunction").Call(C); 24 } 25 catch (Exception) 26 { 27 28 Console.WriteLine("No function"); 29 } 30 luaVM.GetFunction("GetFunction2").Call(C);//調用lua中的GetFunction方法 31 } 32 33 public void LuaSleep(int str) 34 { 35 Console.WriteLine(str); 36 } 37 public void LuaSleep2<T>(int str) 38 { 39 Console.WriteLine(str); 40 } 41 } 42 43 public class A<T> 44 { 45 public T a; 46 }