C# 使用反射获取带有参数的静态方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11117372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:33:35  来源:igfitidea点击:

Using Reflection to get static method with its parameters

c#reflection

提问by newbie_developer

I'm using a public static class and static method with its parameters:

我正在使用公共静态类和静态方法及其参数:

public static class WLR3Logon
{
   static void getLogon(int accountTypeID)
   {}
}

Now I am trying to fetch the method with its parameters into another class and using the following code:

现在我试图将带有参数的方法提取到另一个类中并使用以下代码:

MethodInfo inf = typeof(WLR3Logon).GetMethod("getLogon",
    BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);

int[] parameters = { accountTypeId };

foreach (int parameter in parameters)
{
    inf.Invoke("getLogon", parameters);
}

But its giving me error

但它给了我错误

"Object reference not set to an instance of an object."

“你调用的对象是空的。”

Where I'm going wrong.

我哪里出错了。

采纳答案by newbie_developer

This problem got solved by using the following approach:

通过使用以下方法解决了这个问题:

using System.Reflection;    
string methodName = "getLogon";
Type type = typeof(WLR3Logon);
MethodInfo info = type.GetMethod(
    methodName, 
    BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

object value = info.Invoke(null, new object[] { accountTypeId } );

回答by O. R. Mapper

Your method is private as you have not explicitly declared an access modifier. You have two options to make your code work as intended:

您的方法是私有的,因为您没有明确声明访问修饰符。您有两个选项可以让您的代码按预期工作:

  • Change your method to public.
  • Specify BindingFlags.NonPublicin the GetMethodcall
  • 将您的方法更改为public.
  • BindingFlags.NonPublicGetMethod调用中指定

回答by Talha

make your method public. It should work after that

使你的方法public。它应该在那之后工作

 public static class WLR3Logon
 {
       public static void getLogon(int accountTypeID)
       {}
 }

回答by Polity

There are many problems here

这里有很多问题

  • Your static method is Private yet you Select a method filtered on publicly visible access only. Either make your method public or ensure that the binding flags include private methods. Right now, no method will be found returning in inf being null which causes your null-ref exception.
  • The parameters is an array of ints where MethodInfo expects an array of objects. You need to ensure that you pass in an array of objects.
  • You loop over the parameters only to invoke the method multiple times with the whole parameter set. Remove the loop.
  • You call MethodInfo.Invoke with the name of the method as the first argument which is useless since this parameter is ment for instances when the method was an instance method. In your case, this argument will be ignored
  • 您的静态方法是私有的,但您选择了一个仅对公开可见访问进行过滤的方法。要么公开您的方法,要么确保绑定标志包含私有方法。现在,在 inf 中找不到返回 null 的方法,这会导致您的 null-ref 异常。
  • 参数是一个整数数组,其中 MethodInfo 需要一个对象数组。您需要确保传入一个对象数组。
  • 您循环参数只是为了使用整个参数集多次调用该方法。取下环。
  • 您使用方法名称作为第一个参数调用 MethodInfo.Invoke,这是无用的,因为当方法是实例方法时,此参数用于实例。在您的情况下,此参数将被忽略