给定静态类型的类型名称和方法名称作为字符串,在静态类上调用方法 [英] Calling a method on a static class given its type name and method names as strings
本文介绍了给定静态类型的类型名称和方法名称作为字符串,在静态类上调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请问如何在给定类名和方法名的情况下在静态类上调用方法?
How could I go about calling a method on a static class given the class name and the method name, please?
例如:
给出 System.Environment
和 GetFolderPath
,我想使用 Reflection
调用 Environment.GetFolderPath()
。
Given System.Environment
and GetFolderPath
, I'd like to use Reflection
to call Environment.GetFolderPath()
.
推荐答案
仅
Type.GetType(typeName).GetMethod(methodName).Invoke(null, arguments);
其中 typeName
是类型的名称 methodName
作为字符串,方法的名称为字符串, 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.
这篇关于给定静态类型的类型名称和方法名称作为字符串,在静态类上调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文