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}";
        }
    }
}
相关推荐
妮妮喔妮20 分钟前
Go的垃圾回收
开发语言·后端·golang
向上的车轮3 小时前
无需云服务的家庭相册:OpenHarmony 上的 Rust 实践
开发语言·后端·rust
豐儀麟阁贵4 小时前
4.5数组排序算法
java·开发语言·数据结构·算法·排序算法
Jane-6667774 小时前
C语言——栈与队列
c语言·开发语言
“抚琴”的人4 小时前
C# 取消机制(CancellationTokenSource/CancellationToken)
开发语言·c#·wpf·1024程序员节·取消机制
Halo_tjn4 小时前
Java Map集合
java·开发语言·计算机
lsx2024064 小时前
DOM 创建节点
开发语言
richxu202510015 小时前
C语言<<超全.超重要>>知识点总结
c语言·开发语言
2501_916008895 小时前
用多工具组合把 iOS 混淆做成可复用的工程能力(iOS混淆|IPA加固|无源码混淆|Ipa Guard|Swift Shield)
android·开发语言·ios·小程序·uni-app·iphone·swift
胎粉仔5 小时前
Swift 初阶 —— inout 参数 & 数据独占问题
开发语言·ios·swift·1024程序员节