0

So I have a little lua script where I want to call an extension method on IEnumerable collection.

require ''CLRPackage''
import ''System.Collections.Generic''
import ''System.Linq''
import ''My.Namespace.Containing.AudioMarker''

local audioMarkersWithOffset = GetAudioMarkers();
local numberOfMarkers = audioMarkersWithOffset.Count();

So GetAudioMarkers() is a C# method returning an IEnumerable of AudioMarker objects. Doing a luanet.each will work fine and I will be able to iterate to every element of the collection. But I need the count of that collection and calling .Count() does the following error: NLua.Exceptions.LuaScriptException: [string "chunk"]:54: attempt to call field 'Count' (a string value).

By the way, I know that with nlua you don't need to pre-register your types to used them so I try with and without the last import about AudioMarker, but got the same result.

I'm probably doing something wrong but I cannot seem to find any documentation on the web that could help regarding this issue.

4

1 回答 1

2

我自己一直在尝试使用IEnumerable<T>.ToList()扩展方法,但测试表明 NLua 在泛型方法方面存在一些问题。void func<T>(<T> arg)如果您将其注册为 lua 函数(Lua.RegisterFunction),则可以调用表单的方法,但如果您尝试调用在 lua 状态下存在的对象上使用相同的方法,您会收到“尝试调用方法...”错误。此外,表单的方法void func<T>(IEnumerable<T> arg)在两种情况下都将失败NullReferenceException,分别出现错误和“尝试调用方法...”错误。

还有一点是,如果你想从 Lua 调用 C# 扩展方法,你需要“:”语法,而不是“.”。(参见 NLua“TestExtensionMethods”单元测试)。

于 2015-08-21T14:31:11.397 回答