简体   繁体   English

如何按名称执行静态方法

[英]How to execute static methods by name

I have an XML file with classes name like this: 我有一个带有类名称的XML文件,如下所示:

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

Each of this classes containts a method: bool SingleCheck() ; 每个此类都包含一个方法: bool SingleCheck() ;

I would like to execute this bool SingleCheck() method for each class that is in this XML file. 我想对该XML文件中的每个类执行bool SingleCheck()方法。

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... 我想代表,Actions或Func <>必须在这里...

Thank You very much in advance! 提前非常感谢您!

Edit: Each name in XML points to a separate class. 编辑:XML中的每个名称都指向一个单独的类。 Each class have the same named method: public static bool SingleCheck() . 每个类具有相同的命名方法: 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. 在该列表上存在的每个类中调用一个方法(每个类中的名称相同)。

EDIT - PROBLEM SOLVED: 编辑-解决的问题:

When I first created my project, I included separate folder for all monitors. 最初创建项目时,我为所有监视器都包括了单独的文件夹。 Then I changed my mind, deleted this folder and added manually SAME FILES to my solution. 然后我改变了主意,删除了该文件夹,并手动将“相同文件”添加到了解决方案中。 In this way - those files still had " using <namespace>.Monitors "... And that's why I couldn't list those classes and the Types were still nulls... 这样-这些文件仍然具有“ using <namespace>.Monitors ” ...这就是为什么我无法列出这些类并且Types仍然为null的原因...

Thanks for all suggestions ! 感谢所有建议! ;) ;)

I would suggest to take this overload of the method Invoke It wants an object(calling instance) and a set of input parameters for the method from you. 我建议对方法调用进行重载,它需要您提供一个对象(调用实例)和该方法的一组输入参数。

Since it is a static method, you can calmly pass null as the first parameter and because you method does not have any parameters you again can calmly pass null as the second value. 由于它是静态方法,因此您可以轻松地将null作为第一个参数传递,并且由于您的方法没有任何参数,因此您可以再次轻松地将null作为第二个值传递。 Don't forget to cast object to the corresponding return type. 不要忘记将object转换为相应的返回类型。 In your case bool . 就您而言, bool

bool methodResult = (bool)singleMonitorMethod.Invoke(null, null);

To get the correct Type you actually need to know the namespace! 要获得正确的Type,您实际上需要知道名称空间! So this would look like this: 所以这看起来像这样:

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

If the loop is in the same namespace this should also work: 如果循环在相同的名称空间中,则这也应该起作用:

Type thisType = Type.GetType($"{GetType().Namespace}.{monitorName}");

thisType.GetMethod("{monitorName}.SingleCheck") won't work because of two reasons. 由于以下两个原因, thisType.GetMethod("{monitorName}.SingleCheck")无法正常工作。 1) You forgot the string interpolation $-sign and thus are searching for a method called "{monitorName}.SingleCheck" which obviously can't exist with such a name. 1)您忘记了字符串插值$符号,因此正在搜索一个名为“ {monitorName} .SingleCheck”的方法,该方法显然不存在这样的名称。 2) Instead of thisType you need to provide the type containing the method. 2)您需要提供包含方法的类型来代替thisType

Invoke needs to be called with the instance as first parameter - null for static methods - and an object array for the method parameters. 需要使用实例作为第一个参数来调用调用 -静态方法为null-方法参数为对象数组。

Assuming that your monitor classes are in the same assembly like your current type you would need to do the following: 假设您的监视器类与当前类型位于同一程序集中,则需要执行以下操作:

foreach (string monitorName in monitorsList)
{
  Type monitorType = GetType().Assembly.GetExportedTypes().Single(x => x.Name == monitorName);
  MethodInfo singleMonitorMethod = monitorType.GetMethod("SingleCheck");
  bool methodResult = (bool)singleMonitorMethod.Invoke(null, Array.Empty<object>());
}

I prefer Array.Empty over new object[0] or new object[] { } because it doesn't create a new object every time. 我不喜欢Array.Empty而不是new object[0]new object[] { }因为它不会每次都创建一个新对象。

Edited: Changed the type discovery according to Mong Zhu's comment that GetType(monitorName) does need the fully-qualified name. 编辑:根据Mong Zhu的评论更改了类型发现, GetType(monitorName)确实需要标准名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM