/// <summary> /// 处理参数 /// </summary> private void ProcessParameter(MethodInfo method, object[] args, RequestTemplate requestTemplate, IFeignEncoder encoder) { var parameterInfos = method.GetParameters(); //所有参数里,只能有一个body的注解 var hasBodyAttribute = false; //参数集合 var parameters = new Dictionary <string, string>(); //url var url = requestTemplate.Url; for (var i = 0; i < parameterInfos.Length; i++) { var arg = args[i]; var parameterInfo = parameterInfos[i]; var parameterType = parameterInfos[i].ParameterType; var paramAttribute = parameterInfo.GetCustomAttribute <ParamAttribute>(); var bodyAttribute = parameterInfo.GetCustomAttribute <BodyAttribute>(); var parameterName = parameterInfos[i].Name; if (paramAttribute != null && bodyAttribute != null) { throw new Exception(parameterType.Name + "can not accept parameterAttrite and bodyAttribute"); } if (hasBodyAttribute) { throw new Exception("bodyAttribute just only one"); } if (bodyAttribute != null) { hasBodyAttribute = true; } var parameterTypeIsString = parameterType.IsString(); //处理param类型 if ((parameterTypeIsString || parameterType.IsValueType) && bodyAttribute == null) { parameterName = paramAttribute != null?paramAttribute.Value.GetValueOrDefault(parameterName) : parameterName; parameters.Add(parameterName, arg.ToString()); } //处理body类型 if (!parameterTypeIsString && parameterType.IsClass && bodyAttribute != null) { encoder.Encoder(args[i], requestTemplate); } } var strParam = string.Join("&", parameters.Select(o => o.Key + "=" + o.Value)); if (strParam.HasText()) { url = string.Concat(url, '?', strParam); } requestTemplate.Url = url; }
/// <summary> /// 处理参数 /// </summary> private void ProcessParameter(MethodInfo method, object[] args, RequestTemplate requestTemplate, IFeignEncoder encoder) { var parameterInfos = method.GetParameters(); var multipartAttribute = method.GetCustomAttribute <MultipartAttribute>(); var multipartFormDataContent = new MultipartFormDataContent(); for (var i = 0; i < parameterInfos.Length; i++) { var arg = args[i]; var parameterInfo = parameterInfos[i]; var parameterType = parameterInfos[i].ParameterType; var aliasAsAttribute = parameterInfo.GetCustomAttribute <AliasAsAttribute>(); var bodyAttribute = parameterInfo.GetCustomAttribute <BodyAttribute>(); var queryAttribute = parameterInfo.GetCustomAttribute <QueryAttribute>(); var parameterName = aliasAsAttribute != null ? aliasAsAttribute.Name : parameterInfos[i].Name; //处理body类型 if (bodyAttribute != null) { switch (bodyAttribute.SerializationKind) { case BodySerializationKind.Json: requestTemplate.HttpContent = encoder.Encoder(args[i]); break; case BodySerializationKind.Form: if (arg is string str) { requestTemplate.HttpContent = new StringContent(Uri.EscapeDataString(str), Encoding.UTF8, "application/x-www-form-urlencoded"); } else { var dictionary = new Dictionary <string, string>(); if (arg is IDictionary tempDictionary) { foreach (var key in tempDictionary.Keys) { var value = tempDictionary[key]; if (value != null) { dictionary.Add(key.ToString(), value?.ToString()); } } } else { var parameterPropertyInfos = parameterType.GetProperties(); foreach (var propertyInfo in parameterPropertyInfos) { var propertyAliasAsAttribute = propertyInfo.GetCustomAttribute <AliasAsAttribute>(); var key = propertyAliasAsAttribute != null ? propertyAliasAsAttribute.Name : propertyInfo.Name; var value = propertyInfo.GetValue(arg); if (value != null) { dictionary.Add(key.ToString(), value?.ToString()); } } } if (multipartAttribute == null) { requestTemplate.HttpContent = new FormUrlEncodedContent(dictionary); } else { foreach (KeyValuePair <string, string> pair in dictionary) { multipartFormDataContent.Add(new StringContent(pair.Value), pair.Key); } } } break; } } else if (queryAttribute != null) { if (arg != null) { if (arg is string str || parameterType.IsValueType) { urlParameters.Add(parameterName, arg.ToString()); } else if (parameterType.IsClass) { var parameterPropertyInfos = parameterType.GetProperties(); foreach (var propertyInfo in parameterPropertyInfos) { var propertyAliasAsAttribute = propertyInfo.GetCustomAttribute <AliasAsAttribute>(); var key = propertyAliasAsAttribute != null ? propertyAliasAsAttribute.Name : propertyInfo.Name; var value = propertyInfo.GetValue(arg); if (value != null) { urlParameters.Add(key, value.ToString()); } } } } }