Search code examples
c#.netreflectioninvokedelegation

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?


Solution

  • Finally I get it, I will let the answer for anyone who needs it. It works with static and non-static types. Take in consideration that namespaceClass must be Namespace.etc.Class

        public static void Executar(string namespaceClass, string metodo, List<Parametros> parametros = null)
        {
            Type type = Type.GetType(namespaceClass);
            MethodInfo methodInfo = type.GetMethod(metodo);
            Object objectToInvoke;
            if (type.IsAbstract && type.IsSealed)
            {
                objectToInvoke = type;
            }
            else {
                objectToInvoke = Activator.CreateInstance(type);
            }
    
            ParameterInfo[] parametersFromMethod = methodInfo.GetParameters();
    
    
    
            if (parametros != null || (methodInfo != null && parametersFromMethod != null && parametersFromMethod.Length > 0))
            {
                List<object> myParams = new List<object>();
                foreach (ParameterInfo parameterFound in parametersFromMethod)
                {
                    Parametros parametroEspecificado = parametros.Where(p => p.name == parameterFound.Name).FirstOrDefault();
                    if (parametroEspecificado != null)
                    {
                        myParams.Add(parametroEspecificado.value);
                    }
                    else
                    {
                        myParams.Add(null);
                    }
    
                }
    
                methodInfo.Invoke(objectToInvoke, myParams.ToArray());
    
            }
            else
            {
                methodInfo.Invoke(objectToInvoke, null);
            }
    
    
        }