Exemple #1
0
        /// <summary>
        /// 动态调用指定名称的泛型方法。
        /// </summary>
        /// <param name="genericTypes">泛型</param>
        /// <param name="name">方法的名称。</param>
        /// <param name="args">方法的参数值。</param>
        /// <returns>方法执行的结果,如果方法是一个 void 签名则返回 null 值。</returns>
        public virtual object Call(Type[] genericTypes, string name, params object[] args)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (genericTypes == null)
            {
                throw new ArgumentNullException(nameof(genericTypes));
            }
            if (genericTypes.Length == 0)
            {
                return(Call(name, args));
            }

            var key        = name + genericTypes.Join(t => t.Name + t.MetadataToken, ", ", "[", "]");
            var methodInfo = Type.GetMethod(name, AllBindingFalgs);

            if (methodInfo == null)
            {
                throw new MissingMethodException(Type.FullName, key);
            }
            if (!methodInfo.IsGenericMethodDefinition)
            {
                throw new ArgumentException("仅支持尚未构造泛型参数的方法。", name);
            }

            var d = DynamicFactory.CreateMethodInvoker(methodInfo.MakeGenericMethod(genericTypes));

            return(d(Instance, args));
        }
        private static Tuple <DynamicMethodInvoker, DynamicMethodInvoker> GetInvoker <TResult>(ICommand <TResult> command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            return(Invokers.GetOrAdd(command.GetType(), type =>
            {
                var flags = Reflection.BindingFlags.Static | Reflection.BindingFlags.Public;
                var resultType = typeof(TResult);
                return Tuple.Create(DynamicFactory.CreateMethodInvoker(CommandBusWapperType.MakeGenericType(type, resultType).GetMethod("Execute", flags))
                                    , DynamicFactory.CreateMethodInvoker(CommandBusWapperType.MakeGenericType(type, resultType).GetMethod("ExecuteAsync", flags)));
            }
                                     ));
        }
Exemple #3
0
        /// <summary>
        /// 动态调用指定名称的方法。
        /// </summary>
        /// <param name="name">方法的名称。</param>
        /// <param name="args">方法的参数值。</param>
        /// <returns>方法执行的结果,如果方法是一个 void 签名则返回 null 值。</returns>
        public virtual object Call(string name, params object[] args)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            var methodInfo = Type.GetMethod(name, AllBindingFalgs);

            if (methodInfo == null)
            {
                throw new MissingMethodException(Type.FullName, name);
            }
            var d = DynamicFactory.CreateMethodInvoker(methodInfo);

            return(d(Instance, args));
        }