使用 MoonSharp 在 Lua 中循环使用 C# 字典



我想遍历这样的东西:

public readonly IDictionary<int, Entity> Entities = new Dictionary<int, Entity>();

在Lua中,同时使用MoonSharp。从文档中可以看出,MoonSharp似乎处理了自动转换为IDictionary类型的表格?

但是,尝试做这样的事情...

for k,v in pairs(World.Entities) do
  -- something
end

。给我:

ScriptRuntimeException: bad argument #1 to 'next' (table expected, got userdata)

关于为什么MoonSharp没有将我的字典转换为表格,我错过了什么吗?

谢谢!

我仍然不知道为什么 MoonSharp 没有自动处理这个问题 - 起初我认为这可能是我的"实体"类型的问题,但我已经将其切换并围绕它进行了测试,但仍然没有。

我现在的解决方法是处理.lua文件中的转换。

function convert.dictTable(clr)
  local table = {}
  local enumerator = clr:GetEnumerator()
  while enumerator:MoveNext() do
    table[enumerator.Current.Key] = enumerator.Current.Value
  end
  return table
end

最新更新