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
Calling a method on a static class given its type name and method names as strings
提问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.Environment
and GetFolderPath
, I'd like to use Reflection
to call Environment.GetFolderPath()
.
鉴于System.Environment
和GetFolderPath
,我想用Reflection
来调用Environment.GetFolderPath()
。
采纳答案by Daniel Brückner
Just
只是
Type.GetType(typeName).GetMethod(methodName).Invoke(null, arguments);
where typeName
is the name of the type as a string, methodName
is the name of the method as a string, and arguments
is 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:
以下是您将执行的操作的基本概述:
- Scan all the objects in the current AppDomain - find the one that matches what you know the class name to be
- Get the static method with the name you know on that object
- Dynamically invoke it.
- 扫描当前 AppDomain 中的所有对象 - 找到与您知道的类名匹配的对象
- 使用您在该对象上知道的名称获取静态方法
- 动态调用它。
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.Invoke
and 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 Environment
and using the GetPropery
and GetGetMethod
methods to get the get method of the Environment.CurrentDirectory
property like so;
您在这里所做的是反映命名的类型Environment
并使用GetPropery
andGetGetMethod
方法来获取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;