From 65ffb344ae4a7a9a559261c1ee4c9e40cfc87d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=B9=8F?= Date: Fri, 16 May 2025 16:43:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9list=20=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MethodGeneratorAPI/MethodGeneratorAPI.cs | 174 +++++++++++++++--- 1 file changed, 146 insertions(+), 28 deletions(-) diff --git a/ReZero/SuperAPI/MethodGeneratorAPI/MethodGeneratorAPI.cs b/ReZero/SuperAPI/MethodGeneratorAPI/MethodGeneratorAPI.cs index badf7c6..59bae80 100644 --- a/ReZero/SuperAPI/MethodGeneratorAPI/MethodGeneratorAPI.cs +++ b/ReZero/SuperAPI/MethodGeneratorAPI/MethodGeneratorAPI.cs @@ -255,7 +255,7 @@ namespace ReZero.SuperAPI return; // 无法继续 } - // 获取第一个参数,但使用RawJsonData而不是参数值 + // 获取原始JSON数据 var rawJson = dataModel.RawJsonData; if (string.IsNullOrEmpty(rawJson)) { @@ -266,51 +266,169 @@ namespace ReZero.SuperAPI System.Diagnostics.Debug.WriteLine($"使用JSON数据: {rawJson}"); - var type = methodInfo.GetParameters().First().ParameterType; + // 如果JSON为空,无法继续 + if (string.IsNullOrEmpty(rawJson)) + { + System.Diagnostics.Debug.WriteLine("JSON数据为空,无法处理参数"); + return; + } - // 处理JSON数据 - if (!string.IsNullOrEmpty(rawJson)) + try { - try + // 检查原始JSON是否需要转义处理 + if (rawJson.Contains("\\\"")) { - // 检查原始JSON是否需要转义处理 - if (rawJson.Contains("\\\"")) - { - rawJson = rawJson.Replace("\\\"", "\""); - } - - // 尝试反序列化为目标类型 - parameters[0] = Newtonsoft.Json.JsonConvert.DeserializeObject(rawJson, type); - System.Diagnostics.Debug.WriteLine($"成功将JSON反序列化为: {type.Name}"); + rawJson = rawJson.Replace("\\\"", "\""); } - catch (Exception ex) + + // 解析JSON + JObject jsonObj = null; + try { - System.Diagnostics.Debug.WriteLine($"JSON反序列化失败: {ex.Message}"); + jsonObj = JObject.Parse(rawJson); + System.Diagnostics.Debug.WriteLine($"成功解析JSON对象,属性数量: {jsonObj.Properties().Count()}"); + } + catch + { + System.Diagnostics.Debug.WriteLine("JSON解析失败,尝试使用JsonConvert"); + throw; + } + + // 获取方法参数信息 + var methodParams = methodInfo.GetParameters(); + if (methodParams.Length == 1) + { + // 单参数方法的处理 + var paramInfo = methodParams[0]; + var paramName = paramInfo.Name; + var paramType = paramInfo.ParameterType; - // 如果反序列化失败,尝试其他方式 - try + System.Diagnostics.Debug.WriteLine($"参数名: {paramName}, 类型: {paramType.Name}"); + + // 检查JSON中是否存在与参数同名的属性 + if (jsonObj.ContainsKey(paramName)) { - if (type == typeof(Newtonsoft.Json.Linq.JObject)) + // JSON对象中有与参数同名的属性,使用该属性值 + System.Diagnostics.Debug.WriteLine($"找到与参数同名的JSON属性: {paramName}"); + JToken propValue = jsonObj[paramName]; + + // 转换为参数类型 + try { - parameters[0] = Newtonsoft.Json.Linq.JObject.Parse(rawJson); + if (propValue.Type == JTokenType.Array && + (paramType.IsGenericType && + paramType.GetGenericTypeDefinition() == typeof(List<>))) + { + // 处理数组到List的转换 + string arrayJson = propValue.ToString(); + parameters[0] = JsonConvert.DeserializeObject(arrayJson, paramType); + System.Diagnostics.Debug.WriteLine($"成功将数组属性 {paramName} 转换为List类型"); + } + else + { + // 一般类型转换 + parameters[0] = propValue.ToObject(paramType); + System.Diagnostics.Debug.WriteLine($"成功将属性 {paramName} 转换为 {paramType.Name} 类型"); + } } - else if (type == typeof(Newtonsoft.Json.Linq.JToken)) + catch (Exception propEx) { - parameters[0] = Newtonsoft.Json.Linq.JToken.Parse(rawJson); + System.Diagnostics.Debug.WriteLine($"属性 {paramName} 转换为 {paramType.Name} 失败: {propEx.Message}"); + // 如果属性转换失败,尝试使用整个JSON对象 + try + { + parameters[0] = JsonConvert.DeserializeObject(rawJson, paramType); + System.Diagnostics.Debug.WriteLine($"回退到将整个JSON转换为 {paramType.Name}"); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"JSON转换失败: {ex.Message}"); + // 参数保持默认值 + } } - else + } + else + { + // JSON中没有与参数同名的属性,尝试直接转换整个JSON + System.Diagnostics.Debug.WriteLine($"未找到与参数 {paramName} 同名的JSON属性,尝试转换整个JSON"); + try + { + // 尝试直接将JSON反序列化为参数类型 + parameters[0] = JsonConvert.DeserializeObject(rawJson, paramType); + System.Diagnostics.Debug.WriteLine($"成功将JSON反序列化为: {paramType.Name}"); + } + catch (Exception ex) { - // 最后尝试直接转换或使用默认值 - parameters[0] = Convert.ChangeType(rawJson, type); + System.Diagnostics.Debug.WriteLine($"JSON反序列化失败: {ex.Message}"); + + // 如果反序列化失败,尝试JToken特殊处理 + if (typeof(JToken).IsAssignableFrom(paramType)) + { + if (paramType == typeof(JObject)) + { + parameters[0] = jsonObj; + System.Diagnostics.Debug.WriteLine("直接使用解析后的JObject"); + } + else if (paramType == typeof(JToken)) + { + parameters[0] = jsonObj; + System.Diagnostics.Debug.WriteLine("将JObject转换为JToken"); + } + else + { + System.Diagnostics.Debug.WriteLine($"无法处理的JToken子类型: {paramType.Name}"); + } + } + else + { + System.Diagnostics.Debug.WriteLine($"无法将JSON转换为类型 {paramType.Name}"); + } } } - catch (Exception innerEx) + } + else if (methodParams.Length > 1) + { + // 多参数方法的处理 + System.Diagnostics.Debug.WriteLine($"多参数方法,参数数量: {methodParams.Length}"); + + for (int i = 0; i < methodParams.Length; i++) { - System.Diagnostics.Debug.WriteLine($"参数转换失败: {innerEx.Message}"); - // 保持默认值 + var paramInfo = methodParams[i]; + var paramName = paramInfo.Name; + var paramType = paramInfo.ParameterType; + + // 查找JSON中是否有对应属性 + if (jsonObj.ContainsKey(paramName)) + { + try + { + JToken propValue = jsonObj[paramName]; + parameters[i] = propValue.ToObject(paramType); + System.Diagnostics.Debug.WriteLine($"已绑定参数 {paramName} 为JSON属性值"); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"参数 {paramName} 绑定失败: {ex.Message}"); + } + } } } } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"FillJObjectParameters处理JSON时出错: {ex.Message}"); + + // 回退到原始方法 + try + { + var type = methodInfo.GetParameters().First().ParameterType; + parameters[0] = JsonConvert.DeserializeObject(rawJson, type); + } + catch (Exception fallbackEx) + { + System.Diagnostics.Debug.WriteLine($"回退处理失败: {fallbackEx.Message}"); + } + } } catch (Exception ex) {