public MethodInvokeResult Invoke(object[] param) { try { MyBinder binder = null; if (findmethod == null) { Type[] types = null; if (param != null) { types = new Type[param.Length]; for (int i = 0; i < types.Length; i++) { if (param[i] != null) { types[i] = param[i].GetType(); } else { types[i] = typeof(void); } } } binder = new MyBinder(); var method = instance.GetType().GetMethod(key, System.Reflection.BindingFlags.Public | BindingFlags.Instance, binder, types, null); if (method == null) { throw new NotSupportedException("方法的重载没有找到"); } findmethod = method; } var result = findmethod.Invoke(instance, BindingFlags.Default, binder, param, null); MethodInvokeResult invokeResult = new MethodInvokeResult(); invokeResult.value = result; if (findmethod.ReturnType == typeof(void)) { invokeResult.isVoid = true; } else { invokeResult.isVoid = false; } return(invokeResult); } catch (Exception ex) { throw new NotSupportedException(ex.ToString(), ex); } }
public static MethodInvokeResult Invoke(string typeName, string funName, object[] param) { try { Type type = Type.GetType(typeName); Type[] types = null; if (param != null) { types = new Type[param.Length]; for (int i = 0; i < types.Length; i++) { if (param[i] != null) { types[i] = param[i].GetType(); } else { types[i] = typeof(void); } } } var binder = new MyBinder(); var method = type.GetMethod(funName, System.Reflection.BindingFlags.Public | BindingFlags.Static, binder, types, null); if (method == null) { throw new NotSupportedException("静态方法的重载没有找到"); } var result = method.Invoke(null, BindingFlags.Default, binder, param, null); MethodInvokeResult invokeResult = new MethodInvokeResult(); invokeResult.value = result; if (method.ReturnType == typeof(void)) { invokeResult.isVoid = true; } else { invokeResult.isVoid = false; } return(invokeResult); } catch (Exception ex) { throw new NotSupportedException(ex.ToString(), ex); } }