简体   繁体   English

将C#对象暴露给Lua脚本

[英]Exposing C# objects to Lua scripting

I'm looking into adding scripting to my C# application. 我正在考虑将脚本添加到C#应用程序中。 I've been debating between Lua and C# (through CSharpCodeProvider). 我一直在争论Lua和C#之间(通过CSharpCodeProvider)。

Regardless of which language I use, I need the script to be able to access/manipulate objects/arrays in my main application. 无论使用哪种语言,我都需要脚本来访问/操作主应用程序中的对象/数组。 With C# I should be able to expose my objects and interface functions without too many issues. 使用C#,我应该能够公开我的对象和接口函数,而不会出现太多问题。

However, with Lua it seems like I'll only be able to access the application objects through exposed functions. 但是,使用Lua似乎只能通过公开的函数访问应用程序对象。 I can't see how I could have a non-chunky interface to, for example, arrays. 我看不到如何为数组提供非块状接口。 I'd either need Array1Set(index, value)/Array1Get(index) functions or ArraySet(array_no, index, value)/.... Is there an elegant way to implement this? 我要么需要Array1Set(index,value)/ Array1Get(index)函数,要么需要ArraySet(array_no,index,value)/ ....是否有实现此目标的优雅方法? I don't want to copy the arrays to the Lua machine, manipulate it, then pull it back into my application. 我不想将数组复制到Lua机器上,进行操作,然后将其拉回到我的应用程序中。

Thanks 谢谢

You should take a look at the LuaInterface project, which supports full integration between Lua and .NET. 您应该看一下LuaInterface项目,该项目支持Lua和.NET之间的完全集成。 Ask google for more information about LuaInterface to find lots of useful pages of discussion, samples, and ideas. 向Google询问有关LuaInterface的更多信息,以找到大量有用的讨论,示例和想法页面。

The general method of sharing objects between Lua and any application in any language is to define the __index() and __newindex() metamethods (and possibly others) of a userdata containing either the object instance itself (letting Lua's GC manage the object's lifetime) or a pointer to the instance (which requires careful cooperation with the GC). 的以任何语言共享Lua和任何应用之间的对象的一般方法是定义__index()__newindex()一个的元方法(以及可能其它) userdata包含任一对象实例本身(让Lua的GC管理对象的生存期),或指向实例的指针(需要与GC仔细合作)。 The metamethods allow Lua code to manipulate fields of the object as if they were stored in a Lua table. 元方法使Lua代码可以像存储在Lua表中一样操作对象的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM