Closed
Description
lua从C#拿到一个dictionary,在lua中使用 csharpDic[key]方式去取值的时候,当key类型为string时返回空,当key为int和object时可以正常工作。测试之后发现当类型为string时index函数不会被调用。测试代码如下:
C#代码
public class LuaTemplateTester
{
public class mm<K, V>
{
public K Key;
public V Value;
Dictionary<K, V> dic = new Dictionary<K, V>();
public V this[K key]
{
get
{
Debug.Log($"index function: key={key} key.getType={key.GetType().Name}" +
$" Key.equals(key)={Key.Equals(key)} " +
$" Key.GetHashCode()={Key.GetHashCode()} key.GetHashCode()={key.GetHashCode()}" +
$" dic.ContainsKey(key)={dic.ContainsKey(key)} ");
return Value;
}
}
public mm(K key, V value)
{
this.Key = key;
this.Value = value;
dic.Add(key, value);
}
}
public static mm<int,int> Int2Int=>
new mm<int,int>(10,10);
public static mm<string, string> Str2Str =>
new mm<string, string>("ab", "cd");
public static mm<GObject, string> Gobject2Str =>
new mm<GObject, string>(new GObject() { name = "test" }, "ee");
}
Lua代码:
local v=nil
print("testing int2int")
local i2i = CS.LuaTemplateTester.Int2Int;
v = i2i[10]
print(i2i.Key .. i2i.Value .. (v and "ok" or "nil"))
print("test obj2string")
local o2s = CS.LuaTemplateTester.Gobject2Str;
v = o2s[o2s.Key]
print(type(o2s.Key) .. o2s.Value .. (v and "ok" or "nil"))
print("testing str2str")
local s2s = CS.LuaTemplateTester.Str2Str;
v = s2s["ab"]
print(s2s.Key .. s2s.Value .. (v and "ok" or "nil"))
打印结果:
LUA: testing int2int
index function: key=10 key.getType=Int32 Key.equals(key)=True Key.GetHashCode()=10 key.GetHashCode()=10 dic.ContainsKey(key)=True
LUA: 1010ok
LUA: test obj2string
index function: key=FairyGUI.GObject key.getType=GObject Key.equals(key)=True Key.GetHashCode()=-1996357632 key.GetHashCode()=-1996357632 dic.ContainsKey(key)=True
LUA: userdataeeok
LUA: testing str2str //注意这里没有打印index function
LUA: abcdnil
Metadata
Metadata
Assignees
Labels
No labels