C# 在静态类上调用方法,将其类型名称和方法名称作为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1418209/
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-06 16:19:08  来源:igfitidea点击:

Calling a method on a static class given its type name and method names as strings

c#reflection

提问by Daniel Elliott

How could I go about calling a method on a static class given the class name and the method name, please?

给定类名和方法名,我怎么能在静态类上调用方法呢?

For example:

例如:

Given System.Environmentand GetFolderPath, I'd like to use Reflectionto call Environment.GetFolderPath().

鉴于System.EnvironmentGetFolderPath,我想用Reflection来调用Environment.GetFolderPath()

采纳答案by Daniel Brückner

Just

只是

Type.GetType(typeName).GetMethod(methodName).Invoke(null, arguments);

where typeNameis the name of the type as a string, methodNameis the name of the method as a string, and argumentsis an array of objects containing the arguments to call the method with.

其中typeName是字符串形式的类型名称, 是字符串methodName形式的方法名称,arguments是包含调用方法的参数的对象数组。

回答by George Mauer

Here is a basic outline of what you would do:

以下是您将执行的操作的基本概述:

  1. Scan all the objects in the current AppDomain - find the one that matches what you know the class name to be
  2. Get the static method with the name you know on that object
  3. Dynamically invoke it.
  1. 扫描当前 AppDomain 中的所有对象 - 找到与您知道的类名匹配的对象
  2. 使用您在该对象上知道的名称获取静态方法
  3. 动态调用它。

Edit:This will work if you do not know the namespace of the static class. Otherwise use Daniel Brückner's solution as its much simpler.

编辑:如果您不知道静态类的命名空间,这将起作用。否则,请使用 Daniel Brückner 的解决方案,因为它更简单。

回答by rajesh pillai

System.Reflection.Assembly info = typeof(System.Environment).Assembly;

Type t = info.GetType("System.Environment");
MethodInfo m = t.GetMethod("GetFolderPath");

object result = m.Invoke(null, arguments);

回答by Ahmed Khalaf

First you need to get the Type (by iterating on the assembly using reflection)

首先,您需要获取类型(通过使用反射对程序集进行迭代)

see this link for details: http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx

有关详细信息,请参阅此链接:http: //msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx

or use

或使用

Assembly.GetType

once you have the type in hand you can iterate over members using reflection or

一旦您掌握了类型,您就可以使用反射或

MethodInfo method = typeof(MyClass).GetMethod("MyMethod");

then you can use MethodInfo.Invokeand pass arguments to invoke the method when you want to invoke it.

然后您可以在MethodInfo.Invoke要调用该方法时使用并传递参数来调用它。

回答by Crippledsmurf

What you are doing here is reflecting on the type named Environmentand using the GetProperyand GetGetMethodmethods to get the get method of the Environment.CurrentDirectoryproperty like so;

您在这里所做的是反映命名的类型Environment并使用GetProperyandGetGetMethod方法来获取Environment.CurrentDirectory属性的 get 方法,如下所示;

var getMethod = typeof(Environment).GetProperty("CurentDirectory", BindingFlags.Public | BindingFlags.Static).GetGetMethod();
var currentDirectory = (string)getMethod.Invoke(null, null);

Calling the get method of a property returns it's value and is equivilent to;

调用属性的 get 方法会返回它的值,相当于;

var value = Environment.CurrentDirectory;