Tencent / xLua

xLua is a lua programming solution for C# ( Unity, .Net, Mono) , it supports android, ios, windows, linux, osx, etc.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C#函数,使用in/ref/out修饰的参数,如果再支持默认值,则xlua会报错

ajycode opened this issue · comments

见OverloadMethodWrap类的Init方法
var defalutValue = paramInfos[i].DefaultValue; if (paramInfos[i].IsOptional) { if (defalutValue != null && defalutValue.GetType() != paramInfos[i].ParameterType) { defalutValue = defalutValue.GetType() == typeof(Missing) ? (paramInfos[i].ParameterType.IsValueType() ? Activator.CreateInstance(paramInfos[i].ParameterType) : Missing.Value) : Convert.ChangeType(defalutValue, paramInfos[i].ParameterType); } HasDefalutValue = true; }

如果函数参数支持默认值,则会进入到这段代码,会根据参数类型对默认值做强制转换
但是如果函数参数是被in/out/ref修饰的,假设参数类型是T,则函数参数的实际类型是T&,而默认值参数类型是T,这里做强制转换被失败抛出异常,导致类的后续函数不会被收集。

这里应该再需要加上一条判断语句
defalutValue.GetType().MakeByRefType() != paramInfos[i].ParameterType