mardi 16 juillet 2019

How to execute static methods by name

I have an XML file with classes name like this:

<ActiveMonitorsList>
        <MonitorName>CertificatesMonitor</MonitorName>
        <MonitorName>ServicesMonitor</MonitorName>
        <MonitorName>LogsMonitor</MonitorName>
        <MonitorName>DBMonitor</MonitorName>
</ActiveMonitorsList>

Each of this classes containts a method: bool SingleCheck();

I would like to execute this bool SingleCheck() method for each class that is in this XML file.

What is the best way to do this?

This is what I have so far - it doesn't work:

foreach (string monitorName in monitorsList)
{
    Type thisType = GetType();
    MethodInfo singleMonitorMethod = thisType.GetMethod("{monitorName}.SingleCheck");
    bool methodResult = singleMonitorMethod.Invoke(...);
}

  • In place of (...) - don't know what to put here, but I want to get the result of the method (it's always bool).
  • All of those methods I want to pass as paramters are static.
  • I guess delegates, Actions or Func<> have to go in here...

Thank You very much in advance!

Edit: Each name in XML points to a separate class. Each class have the same named method: public static bool SingleCheck(). What I want to do is:

  • get all the monitors names (classes names will be the same)
  • invoke a method (it has the same name in each class) inside EVERY class present on that list.




Aucun commentaire:

Enregistrer un commentaire