Skip to content

Commit

Permalink
避免 Action 和 Func 类型的委托生成 Convertor 注册代码,减少代码量,节省调用性能
Browse files Browse the repository at this point in the history
  • Loading branch information
m1stm4o authored and liiir1985 committed Jan 29, 2024
1 parent 55178c8 commit 00e5ed7
Showing 1 changed file with 57 additions and 54 deletions.
111 changes: 57 additions & 54 deletions ILRuntime/Runtime/CLRBinding/BindingCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -725,29 +725,15 @@ public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
sb.AppendLine();
}

sb.Append(" app.DelegateManager.RegisterDelegateConvertor<");
sb.Append(realClsName);
sb.AppendLine(">((act) =>");
sb.AppendLine(" {");
sb.Append(" return new ");
sb.Append(realClsName);
sb.Append("((");
first = true;
foreach (var j in miParameters)
if (!realClsName.StartsWith("System.Action<", StringComparison.Ordinal) && !realClsName.StartsWith("System.Func<", StringComparison.Ordinal))
{
if (first)
{
first = false;
}
else
sb.Append(", ");
sb.Append(j.Name);
}
sb.AppendLine(") =>");
sb.AppendLine(" {");
if (mi.ReturnType != typeof(void))
{
sb.Append(" return ((Func<");
sb.Append(" app.DelegateManager.RegisterDelegateConvertor<");
sb.Append(realClsName);
sb.AppendLine(">((act) =>");
sb.AppendLine(" {");
sb.Append(" return new ");
sb.Append(realClsName);
sb.Append("((");
first = true;
foreach (var j in miParameters)
{
Expand All @@ -757,21 +743,54 @@ public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
}
else
sb.Append(", ");
j.ParameterType.GetClassName(out paramClsName, out paramRealClsName, out paramIsByRef);
sb.Append(j.Name);
}
sb.AppendLine(") =>");
sb.AppendLine(" {");
if (mi.ReturnType != typeof(void))
{
sb.Append(" return ((Func<");
first = true;
foreach (var j in miParameters)
{
if (first)
{
first = false;
}
else
sb.Append(", ");
j.ParameterType.GetClassName(out paramClsName, out paramRealClsName, out paramIsByRef);
sb.Append(paramRealClsName);
}
if (!first)
sb.Append(", ");
mi.ReturnType.GetClassName(out paramClsName, out paramRealClsName, out paramIsByRef);
sb.Append(paramRealClsName);
sb.Append(">)act)(");
}
if (!first)
sb.Append(", ");
mi.ReturnType.GetClassName(out paramClsName, out paramRealClsName, out paramIsByRef);
sb.Append(paramRealClsName);
sb.Append(">)act)(");
}
else
{
if (miParameters.Length != 0)
sb.Append(" ((Action<");
else
sb.Append(" ((Action");
{
if (miParameters.Length != 0)
sb.Append(" ((Action<");
else
sb.Append(" ((Action");
first = true;
foreach (var j in miParameters)
{
if (first)
{
first = false;
}
else
sb.Append(", ");
j.ParameterType.GetClassName(out paramClsName, out paramRealClsName, out paramIsByRef);
sb.Append(paramRealClsName);
}
if (miParameters.Length != 0)
sb.Append(">)act)(");
else
sb.Append(")act)(");
}
first = true;
foreach (var j in miParameters)
{
Expand All @@ -781,28 +800,12 @@ public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
}
else
sb.Append(", ");
j.ParameterType.GetClassName(out paramClsName, out paramRealClsName, out paramIsByRef);
sb.Append(paramRealClsName);
}
if (miParameters.Length != 0)
sb.Append(">)act)(");
else
sb.Append(")act)(");
}
first = true;
foreach (var j in miParameters)
{
if (first)
{
first = false;
sb.Append(j.Name);
}
else
sb.Append(", ");
sb.Append(j.Name);
sb.AppendLine(");");
sb.AppendLine(" });");
sb.AppendLine(" });");
}
sb.AppendLine(");");
sb.AppendLine(" });");
sb.AppendLine(" });");

sb.AppendLine(" }");
sb.AppendLine(" }");
Expand Down

0 comments on commit 00e5ed7

Please sign in to comment.