通过C#集合返回的Lua [英] Passing C# collection to back to Lua

查看:251
本文介绍了通过C#集合返回的Lua的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#编写一个DLL所使用Lua脚本。这些脚本要求CLRPackage。到目前为止,我可* load_assembly()*和* import_type()*获得在我的DLL中的类和方法。我回传简单值和字符串,所有的作品。



没有我需要传递一个泛型集合回的Lua。我相信,Lua中将会看到的是一个表,但它不是我清楚如何在C#中的表,并传回。



This 似乎是一个类似的问题,但我没有看到如何实现它。是否有另一种解决方案或者拥有一个精简的代码片段?


解决方案

现在我需要传递泛型集合回的Lua。我相信,Lua中将会看到的是一个表




它不会。 。Lua中会看到一个CLR对象(而,对于CLR对象的用户数据代理)



如果您在C#类有一个方法是这样的:

 公开名单<串GT;的GetList()
{
返回新的List<串GT; {该,酒吧,是,一个};
}



中的Lua侧(您加载模块后,抓起类实例化它因为,说,):

 本地列表= foo的:的GetList ()
打印(列表)

这会给你像 System.Collections.Generic.List1 [System.String]:33476626 。这是一个用户数据,而不是一个表,所以你不能使用接下来来遍历它,你有,因为它是一个C#列表℃下用它进行交互;串>

 本地IT =列表:的GetEnumerator()
,而它:调用MoveNext()做
打印(it.Current)

这是非常丑陋的,非惯用的Lua,以确保(甚至非地道的C#,因为你会使用的foreach 在C#),但我不认为有LuaInterface类型和CLR类型之间的任何自动编组。有点难以启齿; LuaInterface文档是几乎不存在。



您可以编写自己的程序Lua和CLR类型之间进行编组,如:

 函数listToTable(clrlist)
局部T = {}
本地IT = clrlist:的GetEnumerator()
,而它:调用MoveNext()做
T [#T + 1] = it.Current

返回T


...

本地列表= listToTable(富:的GetList( ))
关键,VAL成对(名单)做
打印(KEY,VAL)

添加 dictToTable 键,你会几乎涵盖。


I have a DLL written in C# that is used by Lua scripts. The scripts "require CLRPackage". So far I can *load_assembly()* and *import_type()* to get at the classes and methods in my DLL. I'm passing back simple values and strings, and that all works.

No I need to pass a generic collection back to Lua. I believe that what Lua will see is a table, but it isn't clear to me how to create that table in C# and pass it back.

This seems to be a similar question, but I'm not seeing how to implement it. Is there another solution or one with a stripped down code fragment?

解决方案

Now I need to pass a generic collection back to Lua. I believe that what Lua will see is a table

It won't. Lua will see a CLR object (rather, a userdata proxy for the CLR object).

If you had a method in your C# class like this:

public List<string> GetList()
{
    return new List<string> { "This", "bar", "is", "a" };
}

The Lua side (after you loaded the module, grabbed the class and instantiated it as, say, foo):

local list = foo:GetList()
print(list)

That will give you something like System.Collections.Generic.List1[System.String]: 33476626. This is a userdata, not a table, so you can't use next or pairs to iterate through it, you have to interact with it as it were a C# List<string>:

local it = list:GetEnumerator()
while it:MoveNext() do
  print(it.Current)
end

This is very ugly, non-idiomatic Lua to be sure (even non-idiomatic C#, given that you'd use foreach in C#), but I don't think there's any automatic marshalling between LuaInterface types and CLR types. Kinda hard to tell; LuaInterface documentation is almost nonexistent.

You could write your own routines to marshal between Lua and CLR types, like:

function listToTable(clrlist)
    local t = {}
    local it = clrlist:GetEnumerator()
    while it:MoveNext() do
      t[#t+1] = it.Current
    end
    return t
end
    
...
    
local list = listToTable(foo:GetList())
for key, val in pairs(list) do
  print(key,val)
end

Add a dictToTable and you'd be pretty much covered.

这篇关于通过C#集合返回的Lua的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆