From 4f8152ae11cb01bbc3f81aa431870fcd85d8cf2f Mon Sep 17 00:00:00 2001 From: Phap Dieu Duong Date: Sat, 12 Aug 2023 12:21:44 +0800 Subject: [PATCH] fixed: ExecuteUserActionAsync() broken due to method's params mismatched --- v9/ImageGlass/FrmMain.cs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/v9/ImageGlass/FrmMain.cs b/v9/ImageGlass/FrmMain.cs index 318321d3a..0365a7107 100644 --- a/v9/ImageGlass/FrmMain.cs +++ b/v9/ImageGlass/FrmMain.cs @@ -26,6 +26,7 @@ You should have received a copy of the GNU General Public License using ImageGlass.Settings; using ImageGlass.UI; using System.Diagnostics.CodeAnalysis; +using System.Reflection; using System.Text; using WicNet; @@ -1606,32 +1607,33 @@ public async Task ExecuteUserActionAsync(SingleAction? ac) else if (ac.Executable.StartsWith("IG_")) { // Find the private method in FrmMain - var method = GetType().GetMethod(ac.Executable); - - - // run built-in method - if (method is not null) + if (GetType().GetMethod(ac.Executable) is MethodInfo method) { // check method's params - var paramItems = method.GetParameters(); - var paramters = new List(); + var methodParams = method.GetParameters(); + var paramters = new List(methodParams.Length); - if (paramItems.Length == 1) + + for (var i = 0; i < methodParams.Length; i++) { - object? methodArg = null; - var type = Nullable.GetUnderlyingType(paramItems[0].ParameterType) ?? paramItems[0].ParameterType; + var mParam = methodParams[i]; + var mParamValue = mParam.DefaultValue; + var type = Nullable.GetUnderlyingType(mParam.ParameterType) ?? mParam.ParameterType; + + // get argument value + var argument = ac.Arguments.Skip(i).Take(1).FirstOrDefault(); if (type.IsPrimitive || type.Equals(typeof(string))) { - if (string.IsNullOrEmpty(ac.Arguments.ToString())) + if (string.IsNullOrEmpty(argument?.ToString())) { - methodArg = null; + mParamValue = null; } else { try { - methodArg = Convert.ChangeType(ac.Arguments, type); + mParamValue = Convert.ChangeType(argument, type); } catch (Exception ex) { error = ex; } } @@ -1644,13 +1646,13 @@ public async Task ExecuteUserActionAsync(SingleAction? ac) } - if (methodArg != null && methodArg.GetType().IsArray) + if (mParamValue != null && mParamValue.GetType().IsArray) { - paramters.AddRange((object[])methodArg); + paramters.Add((object[])mParamValue); } else { - paramters.Add(methodArg); + paramters.Add(mParamValue); } }