dimanche 6 octobre 2019

Method invoke is returning 'parameter count mismatch' as error

I want to call a method by string. I got quite far until my code returns parameter count mismatch.

I know that neither my classInstance nor my method is null. I read that you need to put your Parameterarray into another array.

It seems just perfect, but then it gives me this error. The class of the method is located at Modules.Achievements.Achieve. methodname is the variable string that would equal with an existing method in the Achieve class.

            string userid = Modules.Users.GetCreatorId();
            MethodInfo method = typeof(Modules.Achievements.Achieve).GetMethod(methodname);
            try
            {
                if (method == null) { return; }
                object classInstance = Activator.CreateInstance(typeof(Modules.Achievements.Achieve), null);
                if (classInstance == null) { return; }
                object[] parametersArray = new object[] { userid };
                Boolean result = (Boolean)method.Invoke(classInstance, new object[] { parametersArray });
                if (result)
                {
                    Console.WriteLine("succeeded.");
                    return;
                }
                Console.WriteLine("incorrect steamid.");
                return;
            }
            catch (Exception e){ Console.WriteLine($"error:{e.Message}"); }
            Console.WriteLine("failed.");
            return;

My method should return a Boolean if something failed or not. The method only has one Parameter (String). In this case it is the userid that goes into the method as parameter.

Instead of returning anything, it detects an error before returning something. The error is from the method.Invoke(classInstance, new object[] { parametersArray });.

I googled parameter count mismatch on Reflection but all solutions I saw still give the same error. What can I do best?





Aucun commentaire:

Enregistrer un commentaire