Tencent / xLua

xLua is a lua programming solution for C# ( Unity, .Net, Mono) , it supports android, ios, windows, linux, osx, etc.
Other
9.42k stars 2.46k forks source link

如何在 Lua 中获取给定 Dictionary<,> 值的所有键? #1145

Closed svermeulen closed 5 months ago

svermeulen commented 6 months ago

如何在 Lua 中获取给定 Dictionary<,> 值的所有键?

我唯一能让它工作的方式是创建一个像这样的辅助方法:

public static class LuaUtil
{
    public static List<object> ConvertEnumerableToList(IEnumerable values)
    {
        var result = new List<object>();
        foreach (var item in values)
        {
            result.Add(item);
        }

        return result;
    }
}

然后像这样调用它:

local result = LuaUtil.ConvertEnumerableToList(dictionary_value)

for i=0,result.Count-1 do
   local key = result[i]
   print(key)
end

有没有更好的方法来实现这一点?

oOtroyOo commented 5 months ago

just use for k, v in pairs(dic) as usual