ToLua使用原生C#List和Dictionary

介绍

当你用ToLua时C#和Lua之间肯定是会互相调用的,那么lua里面使用List和Dictionary肯定是必然的,在C#中可以调用LuaTable、LuaDictTable、LuArrayTable这三种和List、Dictionary进行互相转换和调用,在Lua里面其实也可以将List和Dictionary转换成LuaTable,不转换当然也可以使用。

Lua中使用原生List

这里我会将一些我用到的基础用法列举一下,List< T >自定义的T也是支持的,这里不具体展示了
下面是两个List表传入到Lua中,在lua中操作两个List

C#调用lua

无GC的调用可以参考我无GC调用lua的文章

        List<int> lst = new List<int>();
        lst.Add(2);
        lst.Add(3);
        lst.Add(4);
        List<int> lst2 = new List<int>();
        lst2.Add(10);
        lst2.Add(11);
        lst2.Add(12);
        //Util.CallMethod("UIMainCityCtrl", "CSharpListTest", lst, lst2);
        Util.NoGCCallMethod<List<int>,List<int>>("UIMainCityCtrl", "CSharpListTest", lst, lst2); 

Lua中操作

这里其实已经把List打了Wrap文件,所以List是可以在lua中直接调用如下所示的一些常规操作


function UIMainCityCtrl.CSharpListTest(CSharpList,lst2)
    CSharpList:Add(555)
    this.LstLog(CSharpList)
    logError(tostring(CSharpList:Contains(555)))
    CSharpList:Remove(555)
    this.LstLog(CSharpList)
    CSharpList:AddRange(lst2)
    this.LstLog(CSharpList)
    CSharpList:RemoveAt(0)
    CSharpList:Insert(0, 123)
    this.LstLog(CSharpList)
    CSharpList:Clear()
    this.LstLog(CSharpList)
end

function this.LstLog(lst)
    logError("Count = "..lst.Count)
    for i=0,lst.Count - 1 do
        logError("lst["..i.."]"..lst[i])
    end
end

打印测试如下

可以结合我上面的代码部分和操作对比一下下面的打印
在这里插入图片描述

Lua中使用原生Dictionary

C#调用lua

无GC的调用可以参考我无GC调用lua的文章

        Dictionary<int,int> dic1 = new Dictionary<int,int>();
        dic1.Add(1, 10);
        dic1.Add(2, 20);
        Dictionary<int,int> dic2 = new Dictionary<int,int>();
        dic2.Add(3, 30);
        dic2.Add(4, 40);
        //Util.CallMethod("UIMainCityCtrl", "CSharpListTest", lst, lst2);
        Util.NoGCCallMethod<Dictionary<int, int>, Dictionary<int, int>>("UIMainCityCtrl", "CSharpListTest", dic1, dic2); 

Lua中操作

这里其实已经把List打了Wrap文件,所以List是可以在lua中直接调用如下所示的一些常规操作


function UIMainCityCtrl.CSharpListTest(dic1,dic2)
    logError("dic1[1] = "..dic1[1])
    logError("dic1[2] = "..dic1[2])
    logError("dic2[3] = "..dic2[3])
    logError("dic2[4] = "..dic2[4])
    this.LstLog(dic1)
    this.LstLog(dic2)
    dic1:Add(3,50)
    dic1:Add(4,60)
    this.LstLog(dic1)
    logError("dic1:ContainsKey()"..tostring(dic1:ContainsKey(1)))
    logError("dic1:ContainsValue()"..tostring(dic1:ContainsValue(3)))
    dic1:Clear()
    this.LstLog(dic1)

end

function this.LstLog(dic)
    logError("Count = "..dic.Count)
    local iter = dic:GetEnumerator()
    while iter:MoveNext() do
        local k = iter.Current.Key
        local v = iter.Current.Value
        logError("k = "..k.." , v = "..v)
    end
end

打印测试如下

可以结合我上面的代码部分和操作对比一下下面的打印
在这里插入图片描述

相关推荐

  1. pytorch 笔记:dist cdist

    2023-12-28 02:04:02       53 阅读
  2. C#面:介绍 Hashtable Dictionary的区别

    2023-12-28 02:04:02       36 阅读
  3. Redis(Remote Dictionary Server)

    2023-12-28 02:04:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 02:04:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 02:04:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 02:04:02       82 阅读
  4. Python语言-面向对象

    2023-12-28 02:04:02       91 阅读

热门阅读

  1. Linux Ubuntu安装nodejs

    2023-12-28 02:04:02       53 阅读
  2. arm day7

    arm day7

    2023-12-28 02:04:02      55 阅读
  3. python虚拟环境及其在项目实践中的应用

    2023-12-28 02:04:02       58 阅读
  4. Sqlserver数据库触发器sql案例

    2023-12-28 02:04:02       63 阅读
  5. Redis——IO多路复用

    2023-12-28 02:04:02       61 阅读
  6. CTE和递归查询

    2023-12-28 02:04:02       55 阅读
  7. Best Arm Identification in Batched Multi-armed Bandit Problems

    2023-12-28 02:04:02       46 阅读
  8. 【Qt-正则表达式】

    2023-12-28 02:04:02       59 阅读
  9. Android Camera相关类功能整理

    2023-12-28 02:04:02       59 阅读
  10. Mybatis之增删查改

    2023-12-28 02:04:02       65 阅读
  11. 【软件测试】面试题之接口测试篇

    2023-12-28 02:04:02       50 阅读
  12. TDD和FDD两种模式下信道估计的主要区别

    2023-12-28 02:04:02       53 阅读
  13. 树莓派安装mariadb & redis

    2023-12-28 02:04:02       53 阅读
  14. Redis过期key清理机制

    2023-12-28 02:04:02       63 阅读