private bool ExcuteInternal(InvokeContextBase context) { try { MethodInfo methodInfo = FindMethodInfo(context); if (methodInfo != null) { object instance = null; if (!context.IsStatic) { instance = ActivatorEx.CreateInstance(methodInfo.ReflectedType); if (instance != null) { context.ReturnValue = methodInfo.Invoke(instance, context.Arguments); } } else { context.ReturnValue = methodInfo.Invoke(null, context.Arguments); } context.Arguments = null; context.ArgumentTypes = null; return(true); } } catch (Exception e) { context.InnerException = e.GetBaseException(); } return(false); }
private static MethodInfoCollection GetMethodCollectionAndCache(InvokeContextBase context, Type type) { MethodCache cache = GetMethodCache(type); MethodInfoCollection infos = GetMethodCollection(cache, type, context.MethodName); if (infos == null) { lock (_syncMethodObject) { infos = GetMethodCollection(cache, type, context.MethodName); if (infos == null) { infos = new MethodInfoCollection(); MemberInfo[] methods = type.GetMembers(_defaultBinding); int index = 0; if (methods != null && methods.Length > 0) { foreach (MemberInfo info in methods) { index++; if (index < 0) { break; } if (info.MemberType == MemberTypes.Method) { MethodInfo method = (MethodInfo)info; MethodAttribute attr = GetMethodAttribute(method); if (attr != null && string.Compare(attr.MethodName, context.MethodName, true) == 0) { infos.Add(method); } else if (string.Compare(info.Name, context.MethodName, true) == 0) { infos.Add(method); } } } } cache[context.MethodName] = infos; } } } return(infos); }
protected static MethodInfo FindMethodInfo(InvokeContextBase context) { if (context != null && !string.IsNullOrEmpty(context.TypeName) && !string.IsNullOrEmpty(context.MethodName)) { Type type = ActivatorEx.GetType(context.TypeName); if (type == null) { return(null); } MethodInfoCollection methods = GetMethodCollectionAndCache(context, type); if (methods != null && methods.Count > 0) { if (methods.Count == 1) { return(CreateMethodEx(methods[0])); } object[] args = context.Arguments; if (args == null) { args = _emptyArgs; context.Arguments = args; } int len = args.Length; Type[] argTypes = context.ArgumentTypes; bool selectedTypes = true; if (argTypes == null) { selectedTypes = false; argTypes = new Type[len]; for (int i = 0; i < len; i++) { if (args[i] != null) { argTypes[i] = args[i].GetType(); } } } BindingFlags binding = _defaultBinding; ArrayList list = new ArrayList(methods.Count); for (int i = 0; i < methods.Count; i++) { if (FilterApplyMethodBaseInfo(methods[i], binding, CallingConventions.Any, argTypes)) { list.Add(methods[i]); } } len = list.Count; if (len >= 1) { MethodInfoEx info = null; if (len == 1) { info = CreateMethodEx(list[0] as MethodInfo); } else { Binder binder = Type.DefaultBinder; MethodBase methodBase = null; MethodBase[] array = new MethodBase[len]; list.CopyTo(array); try { if (!selectedTypes) { object state; methodBase = binder.BindToMethod(binding, array, ref args, null, null, null, out state); } else { methodBase = binder.SelectMethod(binding, array, argTypes, null); } } catch (MissingMethodException) { methodBase = null; } if (methodBase != null) { info = CreateMethodEx(methodBase as MethodInfo); } } return(info); } } } return(null); }
public virtual bool Execute(InvokeContextBase context) { bool result = ExcuteInternal(context); return(result); }