NLua和C#交互

在Lua脚本内引入C# dll:luanet.load_assembly('NLuaTestDll')

在Lua脚本内引入C# 定义的类:TestClass=luanet.import_type('NLuaTestDll.TestClass')

将C#对象导入到Lua脚本:_luaEnv["xxx"] = xxx

将C#中定义的方法导入Lua脚本:

var method = typeof(xLuaForm).GetMethod("StaticMethod");

_luaEnv.RegisterFunction("StaticMethod", null, method);

执行脚本后获取Lua内变量:_luaEnv["xxx"]

cs 复制代码
        public void Print(string msg)
        {
            MessageBox.Show(msg);
        }
        private Lua _luaEnv;
        public xLuaForm()
        {
            InitializeComponent();
            _luaEnv = new Lua();
            
            _luaEnv.LoadCLRPackage();
            //_luaEnv.RegisterLuaClassType(typeof(Person), typeof(Person));
        }
        public static void StaticMethod(string msg)
        {
            MessageBox.Show(msg);
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //_luaEnv.DoString("require 'CSharpCallLua'");
            var script = $@"
    luanet.load_assembly('NLuaTestDll')
    TestClass=luanet.import_type('NLuaTestDll.TestClass')
    Person=luanet.import_type('NLuaTestDll.Person')
    import(""System.Windows.Forms"")
    MessageBox.Show('hello001')
    local newForm = Form()
    newForm:ShowDialog();
    newForm:Dispose();
    person = Person()
    person.Name = 'Winter'
    person.Age = 10
    StaticMethod(person:ToString())
    participant = {{""张三"", ""李四"", ""老王"", ""狗蛋"", ""铁剩""}}
    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["form"] = this;
            var method = typeof(xLuaForm).GetMethod("StaticMethod");
            _luaEnv.RegisterFunction("StaticMethod", null, method);
            _luaEnv.DoString(script);
            var person = _luaEnv["person"] as Person;
            MessageBox.Show(person.Name);
            var addFun = _luaEnv.GetFunction("add");
            var resObj = addFun.Call(10, 20);
 

        }
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NLuaTestDll
{
    public class TestClass : IFoo1
    {
        public int foo()
        {
            return 101;
        }
    }

    public interface IFoo1
    {
        int foo();
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string ToString()
        {
            return $"{Name} -- {Age}";
        }
    }
}
相关推荐
七七&5563 小时前
2024年08月13日 Go生态洞察:Go 1.23 发布与全面深度解读
开发语言·网络·golang
java坤坤3 小时前
GoLand 项目从 0 到 1:第八天 ——GORM 命名策略陷阱与 Go 项目启动慢问题攻坚
开发语言·后端·golang
元清加油4 小时前
【Golang】:函数和包
服务器·开发语言·网络·后端·网络协议·golang
健康平安的活着4 小时前
java之 junit4单元测试Mockito的使用
java·开发语言·单元测试
DjangoJason6 小时前
C++ 仿RabbitMQ实现消息队列项目
开发语言·c++·rabbitmq
m0_480502646 小时前
Rust 入门 KV存储HashMap (十七)
java·开发语言·rust
大阳1236 小时前
线程(基本概念和相关命令)
开发语言·数据结构·经验分享·算法·线程·学习经验
YA3336 小时前
java基础(九)sql基础及索引
java·开发语言·sql
奇树谦7 小时前
QT|windwos桌面端应用程序开发,当连接多个显示器的时候,如何获取屏幕编号?
开发语言·qt
weixin_307779137 小时前
VS Code配置MinGW64编译GNU 科学库 (GSL)
开发语言·c++·vscode·算法