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}";
        }
    }
}
相关推荐
liulilittle43 分钟前
C++/CLI与标准C++的语法差异(一)
开发语言·c++·.net·cli·clr·托管·原生
★YUI★1 小时前
学习游戏制作记录(剑投掷技能)7.26
学习·游戏·unity·c#
daixin88481 小时前
什么是缓存雪崩?缓存击穿?缓存穿透?分别如何解决?什么是缓存预热?
java·开发语言·redis·缓存
你我约定有三1 小时前
RabbitMQ--消息丢失问题及解决
java·开发语言·分布式·后端·rabbitmq·ruby
小乖兽技术1 小时前
C#与C++交互开发系列(二十四):WinForms 应用中嵌入C++ 原生窗体
c++·c#·交互
张北北.1 小时前
【深入底层】C++开发简历4+4技能描述6
java·开发语言·c++
李永奉2 小时前
STM32-定时器的基本定时/计数功能实现配置教程(寄存器版)
c语言·开发语言·stm32·单片机·嵌入式硬件
go54631584652 小时前
中文语音识别与偏误检测系统开发
开发语言·人工智能·学习·生成对抗网络·数学建模·语音识别
NUC_Dodamce2 小时前
Cocos3x 解决同时勾选 适配屏幕宽度和 适配屏幕高度导致Widget组件失效的问题
开发语言·javascript·ecmascript
一杯科技拿铁2 小时前
Go 的时间包:理解单调时间与挂钟时间
开发语言·后端·golang