xlua - c#中LuaTable的Cast和ForEach示例
测试代码
string luaStr = @"
local obj =
{
1,
['a'] = 'A',
2,
['b'] = 'B',
[3.1] = 3.5,
}
return obj
";
var results = m_LuaEnv.DoString(luaStr);
var luaObj = (LuaTable)results[0];
using (luaObj)
{
Debug.Log($"len: {luaObj.Length}"); //结果3
TestX(luaObj);
}
Cast示例1
public static void Test1(LuaTable luaObj)
{
Debug.Log("---------- Test1");
Debug.Log($"len: {luaObj.Length}");
Debug.Log("---------- 1");
var dict1 = luaObj.Cast<Dictionary<string, string>>();
Debug.Log($"keys cnt: {dict1.Count}"); //2
foreach (var entry in dict1)
{
Debug.Log($"{entry}");
}
Debug.Log("---------- 2");
var dict2 = luaObj.Cast<Dictionary<string, object>>();
Debug.Log($"keys cnt: {dict2.Count}"); //2
foreach (var entry in dict2)
{
Debug.Log($"{entry}");
}
Debug.Log("---------- 3");
var dict6 = luaObj.Cast<Dictionary<object, string>>();
Debug.Log($"keys cnt: {dict6.Count}"); //2
foreach (var entry in dict6)
{
Debug.Log($"{entry}");
}
}

Cast示例2
public static void Test2(LuaTable luaObj)
{
Debug.Log("---------- Test2");
Debug.Log("---------- int -> int");
var dict1 = luaObj.Cast<Dictionary<int, int>>();
Debug.Log($"keys cnt: {dict1.Count}");
foreach (var entry in dict1)
{
Debug.Log($"{entry}");
}
Debug.Log("---------- int -> float");
var dict2 = luaObj.Cast<Dictionary<int, float>>();
Debug.Log($"keys cnt: {dict2.Count}");
foreach (var entry in dict2)
{
Debug.Log($"{entry}");
}
Debug.Log("---------- int -> double");
var dict3 = luaObj.Cast<Dictionary<int, double>>();
Debug.Log($"keys cnt: {dict3.Count}");
foreach (var entry in dict3)
{
Debug.Log($"{entry}");
}
Debug.Log("---------- int -> obj");
var dict4 = luaObj.Cast<Dictionary<int, object>>();
Debug.Log($"keys cnt: {dict4.Count}");
foreach (var entry in dict4)
{
Debug.Log($"{entry}");
}
Debug.Log("---------- float -> float");
var dict5 = luaObj.Cast<Dictionary<float, float>>();
Debug.Log($"keys cnt: {dict5.Count}");
foreach (var entry in dict5)
{
Debug.Log($"{entry}");
}
}

Cast示例3
public static void Test3(LuaTable luaObj)
{
Debug.Log("---------- Test3");
Debug.Log("---------- obj -> int");
var dict1 = luaObj.Cast<Dictionary<object, int>>();
Debug.Log($"keys cnt: {dict1.Count}");
foreach (var entry in dict1)
{
Debug.Log($"{entry}");
}
Debug.Log("---------- obj -> obj");
var dict2 = luaObj.Cast<Dictionary<object, object>>();
Debug.Log($"keys cnt: {dict2.Count}");
foreach (var entry in dict2)
{
Debug.Log($"{entry}");
}
}

ForEach示例1
public static void Test5(LuaTable luaObj)
{
Debug.Log("---------- Test5");
Debug.Log("---------- 1");
luaObj.ForEach<string, string>((k, v) =>
{
Debug.Log($"{k}={v}");
});
Debug.Log("---------- 2");
luaObj.ForEach<string, object>((k, v) =>
{
Debug.Log($"{k}={v}");
});
Debug.Log("---------- 3");
luaObj.ForEach<object, string>((k, v) =>
{
Debug.Log($"{k}={v}");
});
}

ForEach示例2
public static void Test6(LuaTable luaObj)
{
Debug.Log("---------- Test6");
Debug.Log("---------- int -> int");
luaObj.ForEach<int, int>((k, v) =>
{
Debug.Log($"{k}={v}");
});
Debug.Log("---------- int -> float");
luaObj.ForEach<int, float>((k, v) =>
{
Debug.Log($"{k}={v}");
});
Debug.Log("---------- int -> double");
luaObj.ForEach<int, double>((k, v) =>
{
Debug.Log($"{k}={v}");
});
Debug.Log("---------- int -> obj");
luaObj.ForEach<int, object>((k, v) =>
{
Debug.Log($"{k}={v}");
});
Debug.Log("---------- float -> float");
luaObj.ForEach<float, float>((k, v) =>
{
Debug.Log($"{k}={v}");
});
}

ForEach示例3
public static void Test7(LuaTable luaObj)
{
Debug.Log("---------- Test7");
Debug.Log("---------- obj -> int");
luaObj.ForEach<object, int>((k, v) =>
{
Debug.Log($"{k}={v}");
});
Debug.Log("---------- obj -> obj");
luaObj.ForEach<object, object>((k, v) =>
{
Debug.Log($"{k}={v}");
});
}


浙公网安备 33010602011771号