コード例 #1
0
ファイル: Invoke.cs プロジェクト: Whiteknight/InvokeWhatever
        public static IInvokeResult InstanceMethod(MethodSortOptions options, object obj, string methodName, params object[] availableArgs)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj), "Must provide an object reference");
            }
            if (string.IsNullOrEmpty(methodName))
            {
                throw new ArgumentException("Must provide a method name to invoke", nameof(methodName));
            }
            availableArgs = availableArgs ?? new object[0];
            var type = obj.GetType();

            var methods = type.GetMethods()
                          .Where(method => method.Name.Equals(methodName, StringComparison.OrdinalIgnoreCase))
                          .Where(method => method.IsPublic && !method.IsStatic)
                          .ToList();

            if (methods.Count == 0)
            {
                return(new MethodNotFoundResult());
            }

            var sorter     = new MethodSorter(options);
            var bestMethod = sorter.GetBestMethod(methods, availableArgs);

            if (bestMethod?.Member == null)
            {
                return(new NoSuitableMethodsResult());
            }
            var methodInfo = bestMethod.Member as MethodInfo;

            if (methodInfo == null)
            {
                return(new NoSuitableMethodsResult());
            }

            if (methodInfo.ReturnType == typeof(void))
            {
                methodInfo.Invoke(obj, bestMethod.Arguments);
                return(new ActionSuccessResult());
            }

            var result = methodInfo.Invoke(obj, bestMethod.Arguments);

            return(new FuncSuccessResult(result));
        }
コード例 #2
0
ファイル: Invoke.cs プロジェクト: Whiteknight/InvokeWhatever
        public static IInvokeResult Constructor(MethodSortOptions options, Type type, params object[] availableArgs)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type), "Must provide a valid type");
            }
            availableArgs = availableArgs ?? new object[0];
            if (type.IsAbstract || type.IsInterface)
            {
                return(new ConstructorNotFoundResult());
            }

            var ctors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);

            if (ctors.Length == 0)
            {
                return(new ConstructorNotFoundResult());
            }

            var sorter   = new MethodSorter(options);
            var bestCtor = sorter.GetBestMethod(ctors, availableArgs);

            if (bestCtor?.Member == null)
            {
                return(new NoSuitableConstructorResult());
            }
            var ctorInfo = bestCtor.Member as ConstructorInfo;

            if (ctorInfo == null)
            {
                return(new NoSuitableConstructorResult());
            }

            var obj = ctorInfo.Invoke(bestCtor.Arguments);

            return(new FuncSuccessResult(obj));
        }