好的,这就是我想要做的:
public static void CallStaticMethod(Type myType, String methodName, Object[] parameters)
{
MethodInfo m = myType.GetMethod(methodName); // make this compile-time safe
m.Invoke(null, parameters); // methodName method is supposed to be static
}
现在,如果“methodName”不存在,myType.GetMethod(methodName) 可能会在运行时失败。
有没有办法让这个编译时安全?
(假设参数正确)
我想以某种方式调用该方法:
CallStaticMethod(()=>MyType.MyMethod(), Object[] parameters)
请注意,这不起作用,因为您需要在 lambda 表达式中指定参数。
换句话说,我需要一个方法的编译时安全句柄。我可以使用 typeof() 获得一个类型,但是方法是否可行?