public void Invoke(string methodname)
{
string funcname=methodname.Split('.')[1];
//funcname contains the value of CustomerController
Type type = typeof(funcname);
MethodInfo method = type.GetMethod(funcname);
CustomerController c = new CustomerController();
string result = (string)method.Invoke(c, null);
}
在这我需要传递一个字符串值到typeof
表达式这是一个类名。但是我得到一个编译时错误。在上面的代码中如何将字符串值传递给一种表达式?
Type type = typeof(funcname);
这里funcname包含值"CustomerController"
。如果我用下面的线代替它,它工作正常。
Type type = typeof(CustomerController);
如何将字符串值传递给typeof
表达式?
你正在调用CustomerController'的'的方法,所以它应该是'typeof运算(CustomerController)'。 – I4V 2013-05-10 11:55:47