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}";
        }
    }
}
相关推荐
weixin199701080165 小时前
《1688消息服务落地方案:alibaba.message.* 与 Webhook 回调的可靠性与重试策略》(附Python源码)
开发语言·python
ZHOUPUYU5 小时前
PHP 9.0 前瞻:下一代 PHP 会带来哪些新功能?
android·开发语言·php
某不知名網友5 小时前
ROS2 核心通信:话题发布与订阅详解
开发语言·c++
C++ 老炮儿的技术栈5 小时前
char (*csConnectName)[256]; 和 char csConnectName [256] 有什么区别
java·c语言·开发语言·c++·人工智能·算法·c
hez20105 小时前
一行代码在 .NET 项目里启用 Satori GC
c#·.net·.net core·gc·clr
Nebula_g5 小时前
JavaSE加强:线程Thread(线程安全重点)
java·开发语言·jvm·算法·javase·技术栈
小灰灰搞电子5 小时前
Rust+Slint 实现一个终端软件源码分享(半成品)
开发语言·rust
大鹏说大话5 小时前
C++ STL 容器怎么选?vector、list、map、unordered_map 对比
开发语言·c++·list
宇卿.6 小时前
C#语句:
开发语言·c#
ComPDFKit6 小时前
用 ComPDF DocSlight 提取 PDF 表格:三种样本实测与 Python 接入
开发语言·python·pdf·pdf表格提取