在一个方法,我想调用哪个名字来作为一个参数,一个共享的功能。
In a method, I want to call a shared function which its name came as a parameter.
例如:
private shared function func1(byval func2 as string)
'call func2
end function
我怎样才能做到这一点?
How can i do it?
您可以使用反射来寻找类和方法。
You can use reflection to find the class and the method.
实例在C#:
Example in C#:
namespace TestProgram {
public static class TestClass {
public static void Test() {
Console.WriteLine("Success!");
}
}
class Program {
public static void CallMethod(string name) {
int pos = name.LastIndexOf('.');
string className = name.Substring(0, pos);
string methodName = name.Substring(pos + 1);
Type.GetType(className).GetMethod(methodName).Invoke(null, null);
}
static void Main() {
CallMethod("TestProgram.TestClass.Test");
}
}
}
上一篇:如何在模板引擎 TWIG 中从完整路径加载模板模板、路径、加载、完整
下一篇:Symfony2 中的 Flash-Messages 似乎在我的 twig-template 中不起作用我的、中不、Flash、Messages