1

我想迭代这样的事情:

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 没有将我的字典转换为表格,我有什么遗漏吗?

谢谢!

4

1 回答 1

1

我仍然不知道为什么 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
于 2018-01-10T05:30:18.730 回答