Exemplo n.º 1
0
        /// <summary>
        /// 参数转换为动态类型
        /// </summary>
        /// <param name="proxyMethodParameter"></param>
        /// <returns></returns>
        private dynamic ArgumentConvert(ProxyMethodParameter proxyMethodParameter)
        {
            //动态参数
            dynamic dynamicParams = new Dictionary <string, object>();

            //多个参数情况包装成字典
            IDictionary <string, object> paramPairs = new Dictionary <string, object>();

            foreach (var parameterInfo in proxyMethodParameter.ParameterInfos)
            {
                object parameterValue = proxyMethodParameter.Arguments[parameterInfo.Position];
                Type   parameterType  = parameterInfo.ParameterType;

                //1.是否一个参数
                if (proxyMethodParameter.Arguments.Length == 1)
                {
                    //如果是值类型
                    if (parameterType.IsValueType)
                    {
                        PathVariable pathVariable = parameterInfo.GetCustomAttribute <PathVariable>();
                        if (pathVariable != null)
                        {
                            //设置路径变量名称
                            paramPairs.Add(pathVariable.Name, proxyMethodParameter.Arguments[parameterInfo.Position]);
                        }
                        else
                        {
                            paramPairs.Add(parameterInfo.Name, proxyMethodParameter.Arguments[parameterInfo.Position]);
                        }
                        dynamicParams = paramPairs;
                    }
                    else
                    {
                        //引用类型,直接动态返回
                        dynamicParams = parameterValue;
                    }
                }
                else
                {
                    //两个及两个以上,全部用字典组装起来
                    PathVariable pathVariable = parameterInfo.GetCustomAttribute <PathVariable>();
                    if (pathVariable != null)
                    {
                        paramPairs.Add(pathVariable.Name, proxyMethodParameter.Arguments[parameterInfo.Position]);
                    }
                    else
                    {
                        paramPairs.Add(parameterInfo.Name, proxyMethodParameter.Arguments[parameterInfo.Position]);
                    }
                    dynamicParams = paramPairs;
                }
            }
            return(dynamicParams);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 客户端代理执行
        /// </summary>
        /// <param name="invocation"></param>
        public void Intercept(IInvocation invocation)
        {
            //1.获取接口方法
            var methodInfo = invocation.Method;

            //2.获取方法上特性
            var attributes = methodInfo.GetCustomAttributes();

            //3.遍历
            foreach (var attribute in attributes)
            {
                //1.获取url
                var type = methodInfo.DeclaringType;
                MicroClient.Attributes.MicroClient microClient = type.GetCustomAttribute <MicroClient.Attributes.MicroClient>();
                if (microClient == null)
                {
                    throw new FrameException("MicroClient 特性不能为空");
                }

                //2.转换成动态参数
                ProxyMethodParameter proxyMethodParameter = new ProxyMethodParameter(methodInfo.GetParameters(), invocation.Arguments);
                dynamic paramPairs = ArgumentConvert(proxyMethodParameter);

                if (attribute is GetPath getPath)
                {
                    //路径变量转换
                    string path = PathParse(getPath.Path, paramPairs);
                    //执行
                    dynamic result = _dynamicMiddlewareService.GetDynamic(microClient.UrlScheme, microClient.ServiceName, path, paramPairs);
                    //获取返回值类型
                    Type returnType = methodInfo.ReturnType;
                    //赋值给返回值
                    invocation.ReturnValue = ResultConvert(result, returnType);
                }
                else if (attribute is PutPath putPath)
                {
                    //路径变量转换
                    string path = PathParse(putPath.Path, paramPairs);
                    //执行
                    dynamic result = _dynamicMiddlewareService.PutDynamic(microClient.UrlScheme, microClient.ServiceName, path, paramPairs);
                    //获取返回值类型
                    Type returnType = methodInfo.ReturnType;
                    //赋值给返回值
                    invocation.ReturnValue = ResultConvert(result, returnType);
                }
                else if (attribute is PostPath postPath)
                {
                    //路径变量转换
                    string path = PathParse(postPath.Path, paramPairs);
                    //执行
                    dynamic result = _dynamicMiddlewareService.PostDynamic(microClient.UrlScheme, microClient.ServiceName, path, paramPairs);
                    //获取返回值类型
                    Type returnType = methodInfo.ReturnType;
                    //赋值给返回值
                    invocation.ReturnValue = ResultConvert(result, returnType);
                }
                else if (attribute is DeletePath deletePath)
                {
                    //路径变量转换
                    string path = PathParse(deletePath.Path, paramPairs);
                    //执行
                    dynamic result = _dynamicMiddlewareService.DeleteDynamic(microClient.UrlScheme, microClient.ServiceName, path, paramPairs);
                    //获取返回值类型
                    Type returnType = methodInfo.ReturnType;
                    //赋值给返回值
                    invocation.ReturnValue = ResultConvert(result, returnType);
                }
                else
                {
                    throw new FrameException("特性方法不存在");
                }
            }
        }