參考鏈接:
https://www.cnblogs.com/chinarbolg/p/9601452.html
luainterface下載(推薦用1.5.3):
http://files.luaforge.net/releases/luainterface/luainterface
0.准備
下載好luainterface后解壓,打開Built文件夾,里面的3個dll后面會用到
創建一個c#控制台項目,添加引用,選擇上面的LuaInterface.dll。然后將上面的lua51.dll和luanet.dll復制到項目中的bin\Debug目錄下
1.創建lua解釋器
1 using LuaInterface; 2 using System; 3 4 namespace TestLua 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 Lua lua = new Lua(); //創建一個lua解釋器 11 lua["num"] = 66; //用C#腳本在lua環境中,創建一個變量 12 lua["str"] = "chinar"; //用C#腳本在lua環境中,創建一個字符串變量 13 Console.WriteLine(lua["num"]); //輸出 14 Console.WriteLine(lua["str"]); //輸出 15 Console.ReadKey(); //等待輸入 16 } 17 } 18 }
輸出:
2.執行lua代碼段(lua.DoString)
1 using LuaInterface; 2 using System; 3 4 namespace TestLua 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 Lua lua = new Lua(); //創建一個lua解釋器 11 lua.DoString("num=666"); //用C#腳本在lua環境中,創建一段lua腳本:num=666 12 lua.DoString("str='chianr666'"); //用C#腳本在lua環境中,創建一段lua腳本:str='chianr666' 13 object[] values = lua.DoString("return num,str"); //用一個object數組 去接受返回值 14 foreach (var value in values) //遍歷 values 數組中的元素 15 { 16 Console.WriteLine(value); //輸出 17 } 18 Console.ReadKey(); //等待輸入 19 } 20 } 21 }
輸出:
3.執行lua代碼文件(lua.DoFile)
1 using LuaInterface; 2 using System; 3 4 namespace TestLua 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 Lua lua = new Lua(); //創建一個lua解釋器 11 lua.DoFile("test.lua"); //加載lua文件 —— lua.DoFile(文件名) 12 Console.ReadKey(); //等待輸入 13 } 14 } 15 }
test.lua
1 print('I am Chianr') 2 MyTable={1,22,333,4444,55555,666666} 3 print(table.concat(MyTable)..'I am Chianr')
注意要將上面的lua文件拖到項目根目錄中,並且修改屬性如下,這樣lua文件才能被識別到
輸出:
4.將c#的方法注冊到lua中,供lua調用(lua.RegisterFunction)
1 using LuaInterface; 2 using System; 3 4 namespace TestLua 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 Lua lua = new Lua(); //創建一個lua解釋器 11 Program program = new Program(); //聲明一個對象 12 13 //lua.RegisterFunction 注冊方法(注冊到Lua中以后方法的名稱,程序對象,程序的類型program.GetType().(傳入C#中的方法名:需要是公有方法)) 14 lua.RegisterFunction("LuaChinarTest", program, program.GetType().GetMethod("ChinarTest")); 15 //然后用lua.DoString(Lua中方法名())調用測試 16 lua.DoString("LuaChinarTest()"); 17 18 //lua.RegisterFunction 注冊靜態方法(注冊到Lua中以后方法的名稱,空,程序的類型 typeof(Program).(傳入C#中的方法名:需要是公有方法)) 19 lua.RegisterFunction("LuaChinarStaticTest", null, typeof(Program).GetMethod("ChinarStaticTest")); 20 //然后用lua.DoString(Lua中靜態方法名())調用測試 21 lua.DoString("LuaChinarStaticTest()"); 22 23 Console.ReadKey(); //等待輸入 24 } 25 26 /// <summary> 27 /// 一個測試方法 28 /// </summary> 29 public void ChinarTest() 30 { 31 Console.WriteLine("這是一個空方法"); 32 } 33 34 /// <summary> 35 /// 一個靜態測試方法 36 /// </summary> 37 public static void ChinarStaticTest() 38 { 39 Console.WriteLine("這個一個靜態空方法"); 40 } 41 } 42 }
輸出:
5.lua調用c#中的類
1 using LuaInterface; 2 using System; 3 4 namespace TestLua 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 Lua lua = new Lua(); //創建一個lua解釋器 11 lua.DoFile("test2.lua"); //Lua引用C#中的類 12 Console.ReadKey(); //等待輸入 13 } 14 15 public int ChinarNum = 60; //整形變量 16 public string Name = "Chinar"; //字符串變量 17 18 /// <summary> 19 /// 用來讓Lua腳本測試的方法 20 /// </summary> 21 public void ChinarTestFun() 22 { 23 Console.WriteLine("你好,Chinar"); 24 } 25 26 /// <summary> 27 /// Ref方法,參數 28 /// </summary> 29 /// <param name="name"></param> 30 /// <param name="count"></param> 31 public void TestRef(string name, ref int count) 32 { 33 Console.WriteLine(name); 34 Console.WriteLine(count); 35 count = name.Length; 36 } 37 38 39 /// <summary> 40 /// Out方法,參數 41 /// </summary> 42 /// <param name="name"></param> 43 /// <param name="count"></param> 44 public void TestOut(string name, out int count) 45 { 46 Console.WriteLine(name); 47 count = name.Length; 48 } 49 } 50 }
test2.lua
1 -- 如果DeBug中有luanet.dll文件,則不需要再次引用 2 -- require "luanet" 3 -- 加載CLR類型("命名空間") 4 luanet.load_assembly("System") 5 -- 加載命名空間下的類 6 Int32 = luanet.import_type("System.Int32") 7 -- 輸出 8 print(Int32) 9 print('- - - - - - - - - - - - - - - - - - - - - - - - - - ') 10 11 12 -- 加載CLR類型("命名空間"ChinarTest) 13 luanet.load_assembly("TestLua") 14 -- 加載命名空間下的類Program 15 Pro=luanet.import_type("TestLua.Program") 16 17 -- 實例化一個類對象 18 pro=Pro() 19 -- 輸出 20 print(pro.ChinarNum) 21 22 -- 調用C#中的方法 23 pro:ChinarTestFun() 24 25 -- 調用Out方法,out參數不需要傳入值,會直接返回類型為一個:表 26 -- 由於C#那邊是一個空返回值的方法,所以第一個返回為空 27 -- 第二個返回 out參數 28 void,stringCount=pro:TestOut("chinar.fun") 29 print(void,stringCount) 30 31 -- Ref參數 32 void1,stringCount1=pro:TestRef("chinar.fun",8) 33 print(void1,stringCount1)
輸出: