samedi 11 mars 2017

c# - Decide method call at run time without having to use reflection

I have a requirement where I need to get a value from database(sometimes even from a .csv file) based on a given value at run time. I implemented it using If-Else as below.

    if(x == "A")
    Method1(); 
else if(x == "B")
Method2();
else if(x == "C")
Method3();

Now the value of x is known at run-time but the value could be any of the already known values like A, B, C so on and so forth.

As the number of possible values for x keep increasing day-by-day, I found out that if-else/switch is creating a performance issue. I tried reflection to invoke the methods (MethodInfo.Invoke()) but maintaining the code has been a tough job using reflection. I am unable to use parameterized contructor to set the values of local variables as they get reset every time MethodInfo.Invoke() is called.

Can someone provide me a way to call methods at run time without having to use reflection?





Aucun commentaire:

Enregistrer un commentaire