1、在C#與Lua相互呼叫
阿新 • • 發佈:2019-02-09
一、利用LuaInterface呼叫lua程式碼
1、下載luainterface,這裡用的luainterface-1.5.3版本。
2、新增 LuaInterface.dll的引用。
3、利用直譯器進行呼叫
- using System;
- using LuaInterface;
- class Program
- {
- staticvoid Main(string[] args)
- {
- Lua lua = new Lua();//建立LUA直譯器
- lua["num"] = 34;
-
Console.WriteLine(lua["num"
- Console.ReadLine();
- }
- }
二、lua.Dostring執行
- using System;
- using LuaInterface;
- class Program
- {
- staticvoid Main(string[] args)
- {
- Lua lua = new Lua();//建立LUA直譯器
- lua.DoString("str = 'a lua string'");
- lua.DoString("num = 2");
-
object
- foreach(object obj in values)
- Console.WriteLine(obj);
- Console.ReadLine();
- }
- }
三、lua.DoFile執行
c#程式碼
- using System;
- using LuaInterface;
- class Program
- {
- staticvoid Main(string[] args)
- {
-
Lua lua = new
- lua.DoFile("mylua.lua");
- Console.ReadLine();
- }
- }
- num = 2;
- str = "lua string"
- print(num,str)
四、將C#普通方法註冊到Lua
- using System;
- using LuaInterface;
- class Program
- {
- staticvoid Main(string[] args)
- {
- Lua lua = new Lua();//建立LUA直譯器
- Program p = new Program();
- lua.RegisterFunction("CLRMethod", p, p.GetType().GetMethod("CLRMethod"));
- lua.DoString("CLRMethod()");
- Console.ReadLine();
- }
- publicvoid CLRMethod()
- {
- Console.WriteLine("CLRMethod is Run");
- }
- }
五、將C#靜態方法註冊到Lua
- using System;
- using LuaInterface;
- class Program
- {
- staticvoid Main(string[] args)
- {
- Lua lua = new Lua();//建立LUA直譯器
- lua.RegisterFunction("CLRMethod", null, typeof(Program).GetMethod("CLRMethod"));
- lua.DoString("CLRMethod()");
- Console.ReadLine();
- }
- publicstaticvoid CLRMethod()
- {
- Console.WriteLine("Static CLRMethod is Run");
- }
- }
六、Lua呼叫C#方法
1、將luanet.dll 放入Debug目錄下
- require "luanet"
- luanet.load_assembly("System");
- Int32 = luanet.import_type("System.Int32");
- num = Int32.Parse("3425")
- print(num)
- print(Int32)
2、例項II,將程式集名稱與名稱空間改成testluainterface
- using LuaInterface;
- namespace testluainterface
- {
- class Program
- {
- staticvoid Main(string[] args)
- {
- Lua lua = new Lua();//建立LUA直譯器
- lua.DoFile("mylua.lua");
- Console.ReadLine();
- }
- publicstring name = "name of program";
- publicvoid TestMethod()
- {
- Console.WriteLine("TestMethod is Run");
- }
- }
- }
- require "luanet"
- luanet.load_assembly("testluainterface")
- Program = luanet.import_type("testluainterface.Program")
- program1 = Program()
- print(program1.name)
- program1:TestMethod()
七、Lua呼叫C#特殊方法(out ref)
1、當函式中有out或ref引數時,out引數和ref引數和函式的返回值一起返回,並且呼叫的時候,out引數不需要傳入
out和ref會作為返回值和c#方法組成一個table一起返回,先返回方法返回值,再返回out或ref
- using System;
- using LuaInterface;
- namespace testluainterface
- {
- class Program
- {
- staticvoid Main(string[] args)
- {
- Lua lua = new Lua();//建立LUA直譯器
- lua.DoFile("mylua.lua");
- Console.ReadLine();
- }
- publicvoid TestOut(string text, outstring msg)
- {
- Console.WriteLine(text);
- msg = "out";
- }
- publicvoid TestRef(string text, refstring msg)
- {
- Console.WriteLine(text);
- msg = "ref";
- }
- }
- }
- require "luanet"
- luanet.load_assembly("testluainterface")
- Program = luanet.import_type("testluainterface.Program")
- program1 = Program()
- print(program1:TestOut("Lua out"))
- print(program1:TestRef("Lua ref","C# ref"))