ToLua使用原生C#List和Dictionary

ToLua是使用原生C#List

介绍

当你用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的文章

csharp 复制代码
        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中直接调用如下所示的一些常规操作

csharp 复制代码
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的文章

csharp 复制代码
        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中直接调用如下所示的一些常规操作

csharp 复制代码
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

打印测试如下

可以结合我上面的代码部分和操作对比一下下面的打印

相关推荐
点云SLAM1 小时前
PyTorch 中Tensor常用数据结构(int, list, numpy array等)互相转换和实战示例
数据结构·人工智能·pytorch·算法·list·numpy·tensor
o0向阳而生0o3 小时前
71、C# Parallel.ForEach 详解
c#
code bean4 小时前
【设计模式】用观察者模式对比事件订阅(相机举例)
观察者模式·设计模式·c#
上位机付工5 小时前
不会PLC,怎么学上位机?
c#·上位机·modbus·三菱·西门子·欧姆龙plc
Kookoos6 小时前
ABP VNext + MongoDB 数据存储:多模型支持与 NoSQL 扩展
后端·mongodb·c#·.net·abp vnext
甄天6 小时前
WPF数据绑定
c#·wpf
牛奶咖啡138 小时前
C#的泛型和匿名类型
c#·泛型接口·泛型方法·泛型委托·泛型类·泛型参数约束·匿名类型
99乘法口诀万物皆可变8 小时前
C#设计模式之AbstractFactory_抽象工厂_对象创建新模式-练习制作PANL(一)
服务器·javascript·c#·html
TomCode先生11 小时前
C# 基础知识总结(带详细文字说明)
开发语言·c#
上位机付工15 小时前
C#上位机通过WebApi访问WinCC
开发语言·c#·上位机·webapi·wincc