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}";
        }
    }
}
相关推荐
至此流年莫相忘5 小时前
设计模式:模板方法模式
java·开发语言·设计模式
土了个豆子的6 小时前
02.继承MonoBehaviour的单例模式基类
开发语言·visualstudio·单例模式·c#·里氏替换原则
疯狂的维修6 小时前
c#中public类比博图
c#·自动化
qq_172805596 小时前
Go 自建库的使用教程与测试
开发语言·后端·golang
久绊A6 小时前
Hydra-SSH 破解安全防范
开发语言·php
阿昭L6 小时前
c++中获取随机数
开发语言·c++
3壹6 小时前
数据结构精讲:栈与队列实战指南
c语言·开发语言·数据结构·c++·算法
悟乙己6 小时前
使用 Python 中的强化学习最大化简单 RAG 性能
开发语言·python·agent·rag·n8n
max5006007 小时前
图像处理:实现多图点重叠效果
开发语言·图像处理·人工智能·python·深度学习·音视频
RickyWasYoung7 小时前
【代码】matlab-遗传算法工具箱
开发语言·matlab