xLua和C#交互

在xLua脚本内加载三方dll:xlua.load_assembly('aLuaTestDll') --不用加.dll后缀

使用C#内部类:CS.xxx.xxx.xxx CS前缀是固定的,后面命名空间和类名按c#调用约定拼写

cs 复制代码
        public xLuaForm()
        {
            InitializeComponent();
            _luaEnv = new LuaEnv();
            //_luaEnv.
        }
        public void Print(string msg)
        {
            MessageBox.Show(msg);
        }
        private LuaEnv _luaEnv;
        public static void StaticMethod(string msg)
        {
            MessageBox.Show(msg);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            var script = $@"
    xlua.load_assembly('NLuaTestDll')
    CS.System.Windows.Forms.MessageBox.Show('hello001')
    local newForm = CS.System.Windows.Forms.Form()
    newForm:ShowDialog();
    newForm:Dispose();
    person = CS.NLuaTestDll.Person()
    person.Name = 'Winter'
    person.Age = 10
    CS.WindowsFormsApp1.xLuaForm.StaticMethod(person:ToString())    
    participant = {{""张三"", ""李四"", ""老王"", ""狗蛋"", ""铁剩""}}
    CS.WindowsFormsApp1.xLuaForm.StaticMethod('abc')
    --require 'CSharpCallLua'
    --person = {{Name = 'Tom', Age = 10}}
    name = 'jerry'
    form.Text = 'lua test'
    form:Print('hello  ' .. #participant)
    function add(a, b)
      return a+b
    end
";
            _luaEnv.Global["form"] = this;
            _luaEnv.DoString(script);
            var p = _luaEnv.Global.Get<Person>("person");
            MessageBox.Show(p.ToString());
        }
cs 复制代码
namespace NLuaTestDll
{
    [LuaCallCSharp]
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string ToString()
        {
            return $"{Name} -- {Age}";
        }
    }
}
相关推荐
hez20103 天前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
雨落倾城夏未凉9 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫10 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫11 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m62511 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户917215619021111 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠12 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫14 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech14 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf16 天前
C#摸鱼实录——IoC与DI案例详解
c#