0

I've got an editor, which reflects methods in a script (and its parameters) and then making them all viewable by the user, making it easy for the end user to invoke methods from the editor.

Example methods would be:

public void TestMethodOne(damage float1, duration float2, ticks float3){}
public void TestMethodTwo(damage float1, walkSpeed float2){}

which in the editor would look like:

TestMethodOne - Damage:[]  Duration:[]   Ticks:[]
TestMethodTwo - Damage:[]  WalkSpeed:[]                   //with [] being user input window

The name of the methods that the use wishes to use are stored as strings, and the parameters are stored in float Lists.

Now I would like to run these methods with the stored variables, without changing their parameters.

Below works if you change them....:

 MethodInfo method = typeof(MethodHolderScript).GetMethod(storedMethodName);
 string[] values = floatParameterList.ToArray();
 object[] args = { values };
 methodName.Invoke(instance, args);

But then I have change the parameters of all of the methods to example:

    public void TestMethodOne(object[] args){}
    public void TestMethodTwo(object[] args){}

Which is not what I want to do. The number of methods and the amount of its float parameters will be updated regularly, and the editor is coded to display the methods values based on its parameter names, so the above "fix" is not an option.

Is there any way to make this work, work around or perhaps another way other than reflection to achieve this?

Mbee3
  • 1
  • 1
  • 1
    Not really clear what you have problem with. Are you looking for alternatives for reflection because you don't care to learn about it or there some other reason? Clearly you should have already searched for https://www.bing.com/search?q=c%23+invoke+method+reflection and found samples like http://stackoverflow.com/questions/2202381/reflection-how-to-invoke-method-with-parameters, so I assume your post is not duplicate of that regular "call method via reflection" answer... Please clarify what help you are looking for. – Alexei Levenkov Dec 28 '15 at 00:20
  • Hi, sorry, I'm bad at explaining things. Basically, I have methods structured like public void TestMethodOne(damage float1, duration float2, ticks float3){} - Is there a way to invoke/run them from a string and a list of floats as parameters without changing the methods themselves? – Mbee3 Dec 28 '15 at 16:20

0 Answers0