Пример #1
0
        /// <summary>
        /// 调用对象的泛型方法
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="methodName"></param>
        /// <param name="generic"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public object Invoker(object instance, string methodName, Type generic, params object[] parameters)
        {
            object returnValue = null;

            ParameterInfo[] parameterInfos  = parameters as ParameterInfo[];
            object[]        parameterValues = null;
            Type[]          parameterTypes  = null;
            GetParameterType(parameterInfos, out parameterValues, out parameterTypes);
            MethodInfo methodInfo = instance.GetType().GetMethod(methodName, parameterTypes);

            if (methodInfo != null)
            {
                methodInfo = methodInfo.MakeGenericMethod(generic);
                Array.Clear(parameterTypes, 0, parameterTypes.Length);
                parameterTypes = null;
                if (methodInfo != null)
                {
                    returnValue = methodInfo.Invoke(instance, parameterValues);
                }
            }
            methodInfo = null;
            RMIInfo rmiInfo = new RMIInfo();

            rmiInfo.ReturnValue     = returnValue;
            rmiInfo.ParameterValues = parameterValues;
            return(rmiInfo);
        }
Пример #2
0
        /// <summary>
        /// 调用类的非泛型方法
        /// </summary>
        /// <param name="type"></param>
        /// <param name="methodName"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public object Invoker(Type type, string methodName, params object[] parameters)
        {
            object returnValue = null;

            ParameterInfo[] parameterInfos  = parameters as ParameterInfo[];
            object[]        parameterValues = null;
            Type[]          parameterTypes  = null;
            GetParameterType(parameterInfos, out parameterValues, out parameterTypes);
            MethodInfo methodInfo = type.GetMethod(methodName, parameterTypes);

            Array.Clear(parameterTypes, 0, parameterTypes.Length);
            parameterTypes = null;
            if (methodInfo != null)
            {
                returnValue = methodInfo.Invoke(null, parameterValues);
            }
            methodInfo = null;
            RMIInfo rmiInfo = new RMIInfo();

            rmiInfo.ReturnValue     = returnValue;
            rmiInfo.ParameterValues = parameterValues;
            return(rmiInfo);
        }