jeudi 20 juillet 2017

c# - How to set the correct order of the parameters invoking with reflection?

I need to call any method from any class (in the same assembly) and passing trought the parameters. So far so good (I believe), but Invoke ask me for an object array (which I can get) but in the same order that is predefined in the method.

I made this class for the parameters:

public  class Parametros {
    public string type { get; set; }
    public string name { get; set; }
    public object value { get; set; }

}

and my method to "invoke" is the following:

    public static void Executar(string namespaceClass, string metodo,List<Parametros> parametros) {
        Type type = Type.GetType(namespaceClass);
        Object obj = Activator.CreateInstance(type);
        MethodInfo methodInfo = type.GetMethod(metodo);
        List<object> myParams = new List<object>();
        foreach (Parametros myparam in parametros) {
            //Get and order the params
            myParams.Add(myparam.value);
        }

        methodInfo.Invoke(obj, myParams.ToArray());
    }

Without the solution of specify the order in my class Parametros, there is any way to accomplishment this, getting the names of the parameters and send it to the invoke method?

Thanks in advance, is the first time I use invoke and this is I get so far.





Aucun commentaire:

Enregistrer un commentaire