//生成响应体 private T GenerateResponseEntity <T>(string xmlBody) where T : class, new() { T instance = new T(); XmlDocument responseDocument = new XmlDocument(); responseDocument.LoadXml(xmlBody); //将响应信息转换成XML格式后存入Dictionary Dictionary <string, object> responseEntity = new Dictionary <string, object>(); foreach (XmlNode child in responseDocument.DocumentElement.ChildNodes) { responseEntity.Add(child.Name, child.InnerText); } //根据T类型的属性特征声明映射名称填充属性值 foreach (PropertyInfo property in typeof(T).GetRuntimeProperties()) { ResponseNameAttribute responseName = property.GetCustomAttribute <ResponseNameAttribute>(); if (responseName == null) { throw new InvalidOperationException(); } if (responseEntity.ContainsKey(responseName.Name)) { property.SetValue(instance, responseEntity[responseName.Name]); } } return(instance); }
//检验响应体的合法性 private bool CheckResponseLegal <T>(T entity) where T : ResponseBase, new() { SortedList <string, string> properties = new SortedList <string, string>(); foreach (PropertyInfo property in typeof(T).GetRuntimeProperties()) { ResponseNameAttribute requestName = property.GetCustomAttribute <ResponseNameAttribute>(); if (requestName == null) { throw new ArgumentOutOfRangeException(nameof(entity)); } object value = property.GetValue(entity); if (value != null) { properties.Add(requestName.Name, value.ToString()); } } string sign = this.CalculateSign(properties, entity.SignType == null ? WechatPaySignType.MD5 : entity.SignType == "MD5" ? WechatPaySignType.MD5 : WechatPaySignType.HMACSHA256 ); return(sign == entity.Sign); }