dimanche 27 décembre 2015

Invoke methods with params

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?





Aucun commentaire:

Enregistrer un commentaire