1、在C#与Lua相互调用

一、利用LuaInterface调用lua代码

1、下载luainterface,这里用的luainterface-1.5.3版本。

2、添加 LuaInterface.dll的引用。

3、利用解释器进行调用

[csharp]  view plain  copy
  1. using System;  
  2. using LuaInterface;  
  3. class Program  
  4. {  
  5.     static void Main(string[] args)  
  6.     {  
  7.         Lua lua = new Lua();//创建LUA解释器  
  8.         lua["num"] = 34;  
  9.         Console.WriteLine(lua["num"]);  
  10.         Console.ReadLine();  
  11.     }  
  12. }  


二、lua.Dostring执行

[csharp]  view plain  copy
  1. using System;  
  2. using LuaInterface;  
  3. class Program  
  4. {  
  5.     static void Main(string[] args)  
  6.     {  
  7.         Lua lua = new Lua();//创建LUA解释器  
  8.         lua.DoString("str = 'a lua string'");  
  9.         lua.DoString("num = 2");  
  10.         object[] values = lua.DoString("return num,str");  
  11.         foreach(object obj in values)  
  12.             Console.WriteLine(obj);  
  13.         Console.ReadLine();  
  14.     }  
  15. }  

三、lua.DoFile执行

c#代码

[csharp]  view plain  copy
  1. using System;  
  2. using LuaInterface;  
  3. class Program  
  4. {  
  5.     static void Main(string[] args)  
  6.     {  
  7.         Lua lua = new Lua();//创建LUA解释器  
  8.         lua.DoFile("mylua.lua");  
  9.         Console.ReadLine();  
  10.     }  
  11. }  
lua代码,PS:要使用ANSI编码保存
[plain]  view plain  copy
  1. num = 2;  
  2. str = "lua string"  
  3. print(num,str)  

四、将C#普通方法注册到Lua

[csharp]  view plain  copy
  1. using System;  
  2. using LuaInterface;  
  3. class Program  
  4. {  
  5.     static void Main(string[] args)  
  6.     {  
  7.         Lua lua = new Lua();//创建LUA解释器  
  8.         Program p = new Program();  
  9.         lua.RegisterFunction("CLRMethod", p, p.GetType().GetMethod("CLRMethod"));  
  10.         lua.DoString("CLRMethod()");  
  11.         Console.ReadLine();  
  12.     }  
  13.   
  14.   
  15.     public void CLRMethod()  
  16.     {  
  17.         Console.WriteLine("CLRMethod is Run");  
  18.     }  
  19. }  

五、将C#静态方法注册到Lua

[csharp]  view plain  copy
  1. using System;  
  2. using LuaInterface;  
  3. class Program  
  4. {  
  5.     static void Main(string[] args)  
  6.     {  
  7.         Lua lua = new Lua();//创建LUA解释器  
  8.         lua.RegisterFunction("CLRMethod"nulltypeof(Program).GetMethod("CLRMethod"));  
  9.         lua.DoString("CLRMethod()");  
  10.         Console.ReadLine();  
  11.     }  
  12.   
  13.     public static void CLRMethod()  
  14.     {  
  15.         Console.WriteLine("Static CLRMethod is Run");  
  16.     }  
  17. }  

六、Lua调用C#方法

1、将luanet.dll 放入Debug目录下

[plain]  view plain  copy
  1. require "luanet"  
  2.   
  3. luanet.load_assembly("System");  
  4.   
  5. Int32 = luanet.import_type("System.Int32");  
  6.   
  7. num = Int32.Parse("3425")  
  8.   
  9. print(num)   
  10.   
  11. print(Int32)   


2、实例II,将程序集名称与命名空间改成testluainterface

[csharp]  view plain  copy
  1. using LuaInterface;  
  2. namespace testluainterface  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             Lua lua = new Lua();//创建LUA解释器  
  9.             lua.DoFile("mylua.lua");  
  10.             Console.ReadLine();  
  11.         }  
  12.         public string name = "name of program";  
  13.         public void TestMethod()  
  14.         {  
  15.             Console.WriteLine("TestMethod is Run");  
  16.         }  
  17.     }  
  18. }  


[plain]  view plain  copy
  1. require "luanet"  
  2.   
  3. luanet.load_assembly("testluainterface")  
  4.   
  5. Program = luanet.import_type("testluainterface.Program")  
  6.   
  7. program1 = Program()  
  8.   
  9. print(program1.name)  
  10.   
  11. program1:TestMethod()  


七、Lua调用C#特殊方法(out ref)

1、当函数中有out或ref参数时,out参数和ref参数和函数的返回值一起返回,并且调用的时候,out参数不需要传入

out和ref会作为返回值和c#方法组成一个table一起返回,先返回方法返回值,再返回out或ref

[csharp]  view plain  copy
  1. using System;  
  2. using LuaInterface;  
  3. namespace testluainterface  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Lua lua = new Lua();//创建LUA解释器  
  10.             lua.DoFile("mylua.lua");  
  11.             Console.ReadLine();  
  12.         }  
  13.   
  14.         public void TestOut(string text, out string msg)  
  15.         {  
  16.             Console.WriteLine(text);  
  17.             msg = "out";  
  18.         }  
  19.   
  20.         public void TestRef(string text, ref string msg)  
  21.         {  
  22.             Console.WriteLine(text);  
  23.             msg = "ref";  
  24.         }  
  25.     }  
  26. }  


[plain]  view plain  copy
  1. require "luanet"  
  2.   
  3. luanet.load_assembly("testluainterface")  
  4.   
  5. Program = luanet.import_type("testluainterface.Program")  
  6.   
  7. program1 = Program()  
  8.   
  9. print(program1:TestOut("Lua out"))  
  10.   
  11. print(program1:TestRef("Lua ref","C# ref"))  
  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值