xLua icon indicating copy to clipboard operation
xLua copied to clipboard

具有重载的方法无法正确将number参数识别为枚举

Open Cuerzor opened this issue 3 years ago • 4 comments

这段代码可以正常运行,自动把1转化为TestEnum.Value1。 C#:

namespace LuaTest
{
    [LuaCallCSharp]
    public enum TestEnum
    {
        Value1 = 1,
        Value2 = 2
    }
    
    [LuaCallCSharp]
    public class TestClass
    {
        public void TestMethod(TestEnum value)
        {
            Debug.Log(value);
        }
    }
}

Lua:

CS.LuaTest.TestClass():TestMethod(1);

但这段代码不行,会抛出异常LuaException: invalid arguments to LuaTest.TestEntity.TestMethod! C#:

namespace LuaTest
{
    [LuaCallCSharp]
    public enum TestEnum
    {
        Value1 = 1,
        Value2 = 2
    }
    
    [LuaCallCSharp]
    public class TestClass
    {
        public void TestMethod(TestEnum value, int arg = -1)
        {
            Debug.Log(value);
        }
    }
}

Lua:

CS.LuaTest.TestClass():TestMethod(1);

猜测是重载方法决定参数类型时,会调用ObjectTranslator的Assignable,但Assignable方法无法将number识别为枚举类型。 而没有重载的方法则不用进行检测。

Cuerzor avatar Jun 22 '22 10:06 Cuerzor

不用猜,文档不是有映射关系么?设计如此。 枚举就对应枚举,而不是number,在C#你用number用在枚举上甚至有编译错误。

chexiongsheng avatar Jun 22 '22 10:06 chexiongsheng

没有默认值时,就没重载判断,直接转换而已。

chexiongsheng avatar Jun 22 '22 11:06 chexiongsheng

枚举有个__CastFrom函数,你可以试试。 CS.LuaTest.TestClass():TestMethod(CS.LuaTest.TestEnum.__CastFrom(1));

chexiongsheng avatar Jun 22 '22 11:06 chexiongsheng

明白了

Cuerzor avatar Jun 22 '22 11:06 Cuerzor