Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nockieboy · Dec 19, 2014 at 08:08 PM · reflectionparametersinvoke

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...

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Dec 19, 2014 at 08:25 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nockieboy · Dec 19, 2014 at 08:26 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

iOS Reflection Code stripping problem 2 Answers

How do I get the system type of Mathf in JS ? 1 Answer

How can I invoke a Method Property from a MethodInfo (Reflection) while keeping it's properties and types generic? 2 Answers

SendMessage vs MethodInfo.Invoke 1 Answer

Undeclared transform array 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges