【发布时间】:2011-04-27 21:37:49
【问题描述】:
它可能与
重复How to dynamically call a class' method in .NET?
和
但以上两个有解决方案,正如答案所说的那样复杂,我猜不适合初学者。
和
两种解决方案都包含“类型”,我认为代码中的“类型”用于定义方法所属的类。
喜欢
static void caller(String myclass, String mymethod)
{
// Get a type from the string
Type type = Type.GetType(myclass);
// Create an instance of that type
Object obj = Activator.CreateInstance(type);
// Retrieve the method you are looking for
MethodInfo methodInfo = type.GetMethod(mymethod);
// Invoke the method on the instance we created above
methodInfo.Invoke(obj, null);
}
但我最初的网站,只包含所有功能通用的一个类,
具有“函数名称”“函数 ID”的数据库
假设:- 函数名称与代码中的完全相同
我只想做到以下几点
根据文本框中提到的id获取函数名的字符串值
现在调用该函数,其名称在字符串变量中
问题
methodinfo,需要“type.GetMethod(mymethod);”
..
【问题讨论】: