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}";
        }
    }
}
相关推荐
Wenweno0o21 小时前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
chenjingming6661 天前
jmeter线程组设置以及串行和并行设置
java·开发语言·jmeter
cch89181 天前
Python主流框架全解析
开发语言·python
不爱吃炸鸡柳1 天前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
十五年专注C++开发1 天前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
Momentary_SixthSense1 天前
设计模式之工厂模式
java·开发语言·设计模式
‎ദ്ദിᵔ.˛.ᵔ₎1 天前
STL 栈 队列
开发语言·c++
勿忘,瞬间1 天前
数据结构—顺序表
java·开发语言
张張4081 天前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_423533991 天前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python