如何运行任何只知道她的完全限定名称的方法

时间:2023-01-02 20:24:32

I've got a name of a method: "Garden.Plugins.Code.Beta.Business.CalculateRest"

我有一个方法的名称:“Garden.Plugins.Code.Beta.Business.CalculateRest”

How to run it? I think about this fancy reflection based solution like RunMethod(string MethodName)

怎么运行呢?我想到了像RunMethod这样的基于反射的解决方案(字符串MethodName)

4 个解决方案

#1


3  

  • Split it into type name and method name by splitting on the last dot
  • 通过拆分最后一个点将其拆分为类型名称和方法名称

  • Get the type using Type.GetType or Assembly.GetType. (Type.GetType will only look in the currently executing assembly and mscorlib, unless you specify the assembly name in the argument.)
  • 使用Type.GetType或Assembly.GetType获取类型。 (Type.GetType只会在当前正在执行的程序集和mscorlib中查找,除非您在参数中指定程序集名称。)

  • Get the method from the type using Type.GetMethod. Assuming it's a public static method, specify BindingFlags.Public | BindingFlags.Static.
  • 使用Type.GetMethod从类型中获取方法。假设它是一个公共静态方法,请指定BindingFlags.Public | BindingFlags.Static。

  • Execute the method by calling MethodInfo.Invoke(null, null). (The first null specifies "no target" - i.e. it's a static method; the second specifies no arguments.)
  • 通过调用MethodInfo.Invoke(null,null)来执行该方法。 (第一个null指定“无目标” - 即它是静态方法;第二个指定无参数。)

If you want to call an instance method or one which takes parameters, you'll need to work out how to get that information too.

如果要调用实例方法或带参数的方法,您还需要弄清楚如何获取该信息。

#2


2  

It's not quite as simple as treating everything to the left of the last dot as the literal typename. If you've got a type of the form:

它并不像将文本类型名称作为最后一个点左边的所有内容一样简单。如果你有一种形式:

X.Y.Z.Type

then it's not necessarily the case that X, Y and Z are namespaces. They could also be types themselves and the subsequent parts could be inner classes:

那么X,Y和Z就不一定是命名空间了。它们也可以是类型本身,后续部分可以是内部类:

class X
{
  class Y
  {
   // etc
  }
}

If this is the case then Type.GetType("X.YU") wont resolve to the Y class.Instead, the clr seperates inner classes with a + symbol, so you'd actually need to call Type.GetType("X+Y");

如果是这种情况,那么Type.GetType(“X.YU”)将不会解析为Y类。相反,clr用+符号分隔内部类,所以你实际上需要调用Type.GetType(“X +” Y“);

If the method that you're calling is a params method then you'll need to so some additional work. You're required to roll the variable parameters up into an array and pass this. You can check for variable parameters by grabbing the ParameterInfo data for a method and seeing if the last parameter has the ParamArray attribute attached.

如果您调用的方法是params方法,那么您将需要进行一些额外的工作。您需要将变量参数向上滚动到数组中并传递它。您可以通过获取方法的ParameterInfo数据并查看最后一个参数是否附加了ParamArray属性来检查变量参数。

#3


1  

It'll be slow, trust me. So don't put it in a critical place.

它会很慢,相信我。所以不要把它放在关键的地方。

Other than that you'll just have to do it "by hand". Start enumerating through all the namespaces, classes, etc. until you find what you need. I don't think there is anything fancy pre-made that does this already. (Although I haven't searched)

除此之外,您只需“手动”完成。开始枚举所有命名空间,类等,直到找到所需内容。我不认为有任何花哨的预制已经做到了这一点。 (虽然我还没有搜索过)

#4


1  

If the type is an instance type:

如果类型是实例类型:

Type.GetType("Garden.Plugins.Code.Beta.Business")
    .GetMethod("CalculateRest").Invoke(myInstanceOfTheType, param1, param2);

If it's a static method:

如果是静态方法:

Type.GetType("Garden.Plugins.Code.Beta.Business")
    .GetMethod("CalculateRest").Invoke(null, param1, param2);

If it doesn't take parameters, just leave off "param1, param2, etc"...

如果它没有参数,只需不要“param1,param2等”......

#1


3  

  • Split it into type name and method name by splitting on the last dot
  • 通过拆分最后一个点将其拆分为类型名称和方法名称

  • Get the type using Type.GetType or Assembly.GetType. (Type.GetType will only look in the currently executing assembly and mscorlib, unless you specify the assembly name in the argument.)
  • 使用Type.GetType或Assembly.GetType获取类型。 (Type.GetType只会在当前正在执行的程序集和mscorlib中查找,除非您在参数中指定程序集名称。)

  • Get the method from the type using Type.GetMethod. Assuming it's a public static method, specify BindingFlags.Public | BindingFlags.Static.
  • 使用Type.GetMethod从类型中获取方法。假设它是一个公共静态方法,请指定BindingFlags.Public | BindingFlags.Static。

  • Execute the method by calling MethodInfo.Invoke(null, null). (The first null specifies "no target" - i.e. it's a static method; the second specifies no arguments.)
  • 通过调用MethodInfo.Invoke(null,null)来执行该方法。 (第一个null指定“无目标” - 即它是静态方法;第二个指定无参数。)

If you want to call an instance method or one which takes parameters, you'll need to work out how to get that information too.

如果要调用实例方法或带参数的方法,您还需要弄清楚如何获取该信息。

#2


2  

It's not quite as simple as treating everything to the left of the last dot as the literal typename. If you've got a type of the form:

它并不像将文本类型名称作为最后一个点左边的所有内容一样简单。如果你有一种形式:

X.Y.Z.Type

then it's not necessarily the case that X, Y and Z are namespaces. They could also be types themselves and the subsequent parts could be inner classes:

那么X,Y和Z就不一定是命名空间了。它们也可以是类型本身,后续部分可以是内部类:

class X
{
  class Y
  {
   // etc
  }
}

If this is the case then Type.GetType("X.YU") wont resolve to the Y class.Instead, the clr seperates inner classes with a + symbol, so you'd actually need to call Type.GetType("X+Y");

如果是这种情况,那么Type.GetType(“X.YU”)将不会解析为Y类。相反,clr用+符号分隔内部类,所以你实际上需要调用Type.GetType(“X +” Y“);

If the method that you're calling is a params method then you'll need to so some additional work. You're required to roll the variable parameters up into an array and pass this. You can check for variable parameters by grabbing the ParameterInfo data for a method and seeing if the last parameter has the ParamArray attribute attached.

如果您调用的方法是params方法,那么您将需要进行一些额外的工作。您需要将变量参数向上滚动到数组中并传递它。您可以通过获取方法的ParameterInfo数据并查看最后一个参数是否附加了ParamArray属性来检查变量参数。

#3


1  

It'll be slow, trust me. So don't put it in a critical place.

它会很慢,相信我。所以不要把它放在关键的地方。

Other than that you'll just have to do it "by hand". Start enumerating through all the namespaces, classes, etc. until you find what you need. I don't think there is anything fancy pre-made that does this already. (Although I haven't searched)

除此之外,您只需“手动”完成。开始枚举所有命名空间,类等,直到找到所需内容。我不认为有任何花哨的预制已经做到了这一点。 (虽然我还没有搜索过)

#4


1  

If the type is an instance type:

如果类型是实例类型:

Type.GetType("Garden.Plugins.Code.Beta.Business")
    .GetMethod("CalculateRest").Invoke(myInstanceOfTheType, param1, param2);

If it's a static method:

如果是静态方法:

Type.GetType("Garden.Plugins.Code.Beta.Business")
    .GetMethod("CalculateRest").Invoke(null, param1, param2);

If it doesn't take parameters, just leave off "param1, param2, etc"...

如果它没有参数,只需不要“param1,param2等”......