当前位置: 首页 > news >正文

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"]

        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 = 10StaticMethod(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+bend
";_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);}
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}";}}
}

http://www.lryc.cn/news/600595.html

相关文章:

  • Flask input 和datalist结合
  • VTK交互——ImageClip
  • xLua和C#交互
  • 高性能网络DPDK、RDMA、XDP初探
  • 电子电气架构 --- 高阶智能驾驶对E/E架构的新要求
  • 工具 | 解决 VSCode 中的 Delete CR 问题
  • uniapp+vue3——通知栏标题纵向滚动切换
  • 全球化2.0 | 云轴科技ZStack亮相阿里云印尼国有企业CXO专家活动
  • 以太坊下一阶段的关键——隐私
  • DSP在CCS中实现双核在线仿真调试及下载的方法(以TMS320F28x为例)
  • 生产环境使用云服务器(centOS)部署和使用MongoDB
  • (React入门上手——指北指南学习(第一节)
  • docker 从主机复制文件到容器外进行编辑
  • MongoDB数据模型
  • vulhub Web Machine(N7)靶场攻略
  • AutoDL 数据盘清理指南:彻底删除 .Trash-0 内文件释放空间
  • “Datawhale AI夏令营”「结构化数据的用户意图理解和知识问答挑战赛」1
  • 网络资源模板--基于Android Studio 实现的简易购物App
  • Java高级之基于Java Attach与Byte-Buddy实现SQL语句增强
  • HDMI-IN调试:双MIPI支持4K60方案
  • 大众化餐饮:把日常过成诗
  • Zama+OpenZeppelin:将机密智能合约带入 DeFi 和数字资产领域
  • 什么是Paimon?Paimon是什么?
  • Claude 4.0 终极编程指南:模型对比、API配置与IDE集成实战
  • ArkData-关系型数据库
  • SWC 深入全面讲解
  • Spring Boot SSE实战:SseEmitter实现多客户端事件广播与心跳保活
  • STM32概况
  • AI记忆问题解决方案Basic Memory
  • 实验-OSPF多区域