Skip to content

Commit

Permalink
fixed: ExecuteUserActionAsync() broken due to method's params mismatched
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Aug 12, 2023
1 parent a0698cd commit 4f8152a
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions v9/ImageGlass/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<object>();
var methodParams = method.GetParameters();
var paramters = new List<object?>(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; }
}
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 4f8152a

Please sign in to comment.