Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

xlua访问用Dicitonary<string,string>拿不到值 #707

Closed
zzx-cc opened this issue Feb 28, 2020 · 4 comments · May be fixed by #965
Closed

xlua访问用Dicitonary<string,string>拿不到值 #707

zzx-cc opened this issue Feb 28, 2020 · 4 comments · May be fixed by #965

Comments

@zzx-cc
Copy link

zzx-cc commented Feb 28, 2020

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
@chexiongsheng
Copy link
Collaborator

建议修改metatable添加这功能
对于C#类的metatable修改可以参考这:https://github.com/Tencent/xLua/blob/master/Assets/XLua/Examples/12_ReImplementInLua/ReImplementInLua.cs#L99

@Domain
Copy link
Contributor

Domain commented Nov 22, 2021

建议修改metatable添加这功能 对于C#类的metatable修改可以参考这:https://github.com/Tencent/xLua/blob/master/Assets/XLua/Examples/12_ReImplementInLua/ReImplementInLua.cs#L99

我尝试了一下,得到的是死循环:
我C#里有一个类:
class CMyTable : IDictionary<string, object>
然后在lua中:
local mt = {
__index = function(o, k)
return o:get_Item(k)
end,
__newindex = function(o, k, v)
o:set_Item(k, v)
end
}
xlua.setmetatable(CS.CMyTable, mt)
结果:mytable.Hello = 'World'
会导致stack overflow:
stack traceback:
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
...
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:8: in metamethod '__index'
init:11: in metamethod '__newindex'

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants