Ejemplo n.º 1
0
        /// <summary>
        /// 创建委托
        /// </summary>
        /// <param name="service">继承NarutoWebSocketService的服务</param>
        /// <param name="action">执行的方法</param>
        /// <param name="parameterEntity">方法的参数</param>
        /// <param name="parameterType">参数类型</param>
        /// <param name="isParamter">是否为有参数的</param>
        /// <returns></returns>
        private static Task Create(object service, string action, bool isParamter, object parameterEntity, Type parameterType)
        {
            //定义输入参数
            var p1 = Expression.Parameter(service.GetType(), "service");
            //方法的参数对象
            var methodParameter = Expression.Parameter(isParamter ? parameterType : typeof(object), "methodParameter");

            //动态执行方法
            var methodCacheInfo = MethodCache.Get(service.GetType(), action);
            //调用指定的方法
            MethodCallExpression actionCall = null;

            //验证是否方法是否 有参数
            if (methodCacheInfo.ParameterInfos.Count() == 0)
            {
                //执行无参方法
                actionCall = Expression.Call(p1, methodCacheInfo.Method);
            }
            else
            {
                //执行有参的方法
                actionCall = Expression.Call(p1, methodCacheInfo.Method, methodParameter);
            }
            //生成lambda
            var lambda = Expression.Lambda(actionCall, new ParameterExpression[] { p1, methodParameter });
            //获取key
            var key = service.GetType().Name + action;

            //存储
            exec.TryAdd(key, lambda.Compile());

            return(exec[key].DynamicInvoke(service, parameterEntity) as Task);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 执行接收的外部消息
        /// </summary>
        /// <param name="service"></param>
        /// <param name="currentServiceType">当前上下文的类型</param>
        /// <param name="messageModel">消息模型</param>
        /// <returns></returns>
        private async Task EexecReciveMessage(object service, Type currentServiceType, WebSocketMessageModel messageModel)
        {
            //获取方法
            var methodCacheInfo = MethodCache.Get(currentServiceType, messageModel.action);
            //获取方法的参数
            var parameters = methodCacheInfo.ParameterInfos;

            //是否含有参数
            var isParamater = parameters.Count() > 0;
            //参数信息
            var parameterEntity = parameters.Count() > 0 ?await messageModel.message?.ToString().ToDeserializeAsync(parameters[0].ParameterType) : null;

            //执行操作
            await NarutoWebSocketServiceExpression.ExecAsync(service, messageModel.action, isParamter : isParamater, parameterEntity : parameterEntity, parameterType : isParamater?parameters[0].ParameterType : default).ConfigureAwait(false);
        }