Skip to content

Commit

Permalink
由于代码生成量的考虑,interface扩展方法不支持obj:foo()语法糖,但是interface扩展方法应该还是能支持通过静态函数的…
Browse files Browse the repository at this point in the history
…方式访问。
  • Loading branch information
chexiongsheng committed Dec 1, 2017
1 parent da96c9f commit 4e84469
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Assets/XLua/Doc/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,9 @@ f1(obj, 1, 2) --调用short版本,成员方法,所以要传对象,静态
f2(obj, 1, 2) --调用int版本
~~~

注意:xlua.tofunction由于使用不太方便,以及使用了反射,所以建议做作为临时方案,尽量用封装的方法来解决。
注意:xlua.tofunction由于使用不太方便,以及使用了反射,所以建议做作为临时方案,尽量用封装的方法来解决。

## 支持interface扩展方法么?

考虑到生成代码量,不支持通过obj:ExtentionMethod()的方式去调用,支持通过静态方法的方式去调用CS.ExtentionClass.ExtentionMethod(obj)

6 changes: 3 additions & 3 deletions Assets/XLua/Src/Editor/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,12 @@ static void getClassInfo(Type type, LuaTable parameters)

//warnning: filter all method start with "op_" "add_" "remove_" may filter some ordinary method
parameters.Set("methods", type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.IgnoreCase | BindingFlags.DeclaredOnly)
.Where(method => !method.IsDefined(typeof (ExtensionAttribute), false) || method.DeclaringType != type)
.Where(method => !method.IsDefined(typeof (ExtensionAttribute), false) || method.GetParameters()[0].ParameterType.IsInterface || method.DeclaringType != type)
.Where(method => methodNames.ContainsKey(method.Name)) //GenericMethod can not be invoke becuase not static info available!
.Concat(extension_methods)
.Where(method => !IsDoNotGen(type, method.Name))
.Where(method => !isMethodInBlackList(method) && (!method.IsGenericMethod || extension_methods.Contains(method) || isSupportedGenericMethod(method)) && !isObsolete(method) && !method.Name.StartsWith("op_") && !method.Name.StartsWith("add_") && !method.Name.StartsWith("remove_"))
.GroupBy(method => (method.Name + ((method.IsStatic && !method.IsDefined(typeof (ExtensionAttribute), false)) ? "_xlua_st_" : "")), (k, v) =>
.GroupBy(method => (method.Name + ((method.IsStatic && (!method.IsDefined(typeof (ExtensionAttribute), false) || method.GetParameters()[0].ParameterType.IsInterface)) ? "_xlua_st_" : "")), (k, v) =>
{
var overloads = new List<MethodBase>();
List<int> def_vals = new List<int>();
Expand Down Expand Up @@ -345,7 +345,7 @@ static void getClassInfo(Type type, LuaTable parameters)
}
return new {
Name = k,
IsStatic = overloads[0].IsStatic && !overloads[0].IsDefined(typeof(ExtensionAttribute), false),
IsStatic = overloads[0].IsStatic && (!overloads[0].IsDefined(typeof(ExtensionAttribute), false) || overloads[0].GetParameters()[0].ParameterType.IsInterface),
Overloads = overloads,
DefaultValues = def_vals
};
Expand Down
3 changes: 3 additions & 0 deletions Assets/XLua/Src/Editor/Template/TemplateCommon.lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ function MethodParameters(method)
return method:GetParameters()
else
local parameters = method:GetParameters()
if parameters[0].ParameterType.IsInterface then
return parameters
end
local ret = {}
for i = 1, parameters.Length - 1 do
ret[i - 1] = parameters[i]
Expand Down

0 comments on commit 4e84469

Please sign in to comment.