mercredi 23 mars 2016

How can I get a form reference from a class by name using reflection in C#?

I have a public static class called Router with some form references like this;

public static class Router
{
     static frmLogin rLogin = new frmLogin();
     static frmDashboard rDashboard = new frmDashboard();
     static frmReport rReport new frmReport();
}

I am trying to create a function called "Route" - this function will take a string parameter, which will match at least one of those form references. When matched, the selected form will have its "Index()" method invoked. (Every form has an Index() pre-built, I just need it called)

There's not too much to say about the purpose of this other than the fact that I'm creating an MVC-like structure for a very specific reason. So any other suggestions may be appreciated.


To summarise, I need to find the correct Form reference by name i.e. "rLogin" using a parameter passed into the "Route" method, which will eventually call the "Index()" function on that form.

I understand this may be possible with Reflection and I got a bit confused with other examples.

Thanks.





Aucun commentaire:

Enregistrer un commentaire