- Home /
Reflection - passing parameters to an invoked function
Hallo,
I'm playing around with reflection and learning the ropes, as it were. I'm trying to invoke a function using reflection, as below:
ParameterInfo[] parameters = methodInfo.GetParameters ();
if (parameters.Length == 0) {
result = methodInfo.Invoke (comp, null);
} else {
object[] parametersArray = args.Split ('|').Select (str => str.Trim ()).ToArray ();
result = methodInfo.Invoke (comp, parametersArray);
}
What this aims to do is call a function without parameters if no arguments are specified, or call one WITH parameters if arguments are specified (as a string, delimited with | symbols.)
If arguments are specified, the args string is split by the | symbol into parametersArray and passed to the invoked function as above.
The problem I'm getting is that the last line of code, where the function is invoked, is generating an error - ArgumentException: failed to convert parameters.
Is this because I am passing parametersArray, which is a string/object, to a function (for testing purposes) that needs a boolean?
The code works fine for a function requiring a single string parameter...
Well, what method do you want to call? What parameter types does it have? Only string parameters? If the method you want to call looks like:
void Some$$anonymous$$ethod1(string s1)
void Some$$anonymous$$ethod2(string s1, string blubb)
void Some$$anonymous$$ethod3(string s1, string foo, string bar)
void Some$$anonymous$$ethod4( )
void Some$$anonymous$$ethod5(string s1,string s2,string s3,string s4,string s5)
Your method should work as long as the parameter count matches your string count in your splitted string array.
If the method you want to call requires any other type than string (for example: int, float, bool, char, Vector3, ....) you can't do it that way since you have to pass the correct parameter types.
Answer by .Talon · Dec 19, 2014 at 08:20 PM
When the function requires a bool parameter you need to invoke it with an array containing one bool variable. Passing a string array won't work if it expects a bool value (failed to convert means can't use string where bool is required here).
Yes I thought that would be the case, just needed confirmation. Great. $$anonymous$$eans I have to check the type of the required parameter/s and then cast the arguments as the appropriate types - making it more complicated than I was hoping.
Answer by Kurdle_4855 · Aug 16, 2016 at 07:18 PM
Hey - I'm having a similar problem, but these answers don't work for me. Here's my code:
I'm calling this: info.Invoke(methodInfo[0], new object[] { args });
On this: void Equip(object[] args) {
} And I get an ArgumentException: failed to convert parameters error. methodInfo is a string array. The [0] element is Equip, and args is an object array.
Your answer
Follow this Question
Related Questions
iOS Reflection Code stripping problem 2 Answers
How do I get the system type of Mathf in JS ? 1 Answer
SendMessage vs MethodInfo.Invoke 1 Answer
Undeclared transform array 0 Answers