mardi 28 juin 2022

Alternate way to call method

I have a generic method that accepts two parameters string and object. It is supposed to invoke a method based on string name. The other parameter is received as object which is basically object parameter of the target method.

I am currently maintaining a dictionary that keeps dictionary against passed string and the actual method. Like

public static Dictionary<string, string> ActionMappings = new Dictionary<string, string>
   {
       { "Get Patients", "Patient.GetPatients" },
       /// other mapping...
    };

I am currently invoking the method based on ActionMappings dictionary. I am using reflection like

string className = methodDetailsValues.Split('.')[0];
string methodName = methodDetailsValues.Split('.')[1];
currMethodClass = Type.GetType("NameSpace" + className);


ConstructorInfo stringConstructor = allConstructorsOfClass.First();
object newStringConstructor =  stringConstructor.Invoke(null);
MethodInfo voidMethodInfo = currMethodClass.GetMethod(methodName);
object jsonString =       voidMethodInfo.Invoke(newStringConstructor, new object[] { });

Can I maintain a list of initialized methods? It is working but it seems to be slow in performance.





Aucun commentaire:

Enregistrer un commentaire