/** * Convert all params of body other than type of readable into content * @param body source Model * @param content target Model * @return void */ public static void Convert(TeaModel body, TeaModel content) { Dictionary <string, object> dict = new Dictionary <string, object>(); Type type = body.GetType(); PropertyInfo[] properties = type.GetProperties(); for (int i = 0; i < properties.Length; i++) { PropertyInfo p = properties[i]; var propertyType = p.PropertyType; if (!typeof(Stream).IsAssignableFrom(propertyType)) { dict[p.Name] = p.GetValue(body); } } string jsonStr = JsonConvert.SerializeObject(dict); TeaModel tempModel = (TeaModel)JsonConvert.DeserializeObject(jsonStr, content.GetType()); Type outType = content.GetType(); PropertyInfo[] outPropertyies = outType.GetProperties(); foreach (PropertyInfo p in outPropertyies) { var outPropertyType = p.PropertyType; p.SetValue(content, p.GetValue(tempModel)); } }
/// <summary> /// 判断一个请求返回的响应是否成功 /// </summary> /// <param name="response">响应对象</param> /// <returns>true:成功;false:失败</returns> public static bool Success(TeaModel response) { PropertyInfo propertyInfo = response.GetType().GetProperty(SUB_CODE_FIELD_NAME); if (propertyInfo == null) { //没有SubCode属性的响应对象,通常是那些无需跟网关远程通信的API,只要本地执行完成都视为成功 return(true); } string subCode = (string)propertyInfo.GetValue(response); return(string.IsNullOrEmpty(subCode)); }
private static void GetXml(TeaModel model, XElement element) { Type type = model.GetType(); PropertyInfo[] properties = type.GetProperties(); for (int i = 0; i < properties.Length; i++) { PropertyInfo propertyInfo = properties[i]; Type property = propertyInfo.PropertyType; NameInMapAttribute attribute = propertyInfo.GetCustomAttribute(typeof(NameInMapAttribute)) as NameInMapAttribute; string realName = attribute == null ? propertyInfo.Name : attribute.Name; XElement node = new XElement(realName); GetXmlFactory(propertyInfo.GetValue(model), node, element); } }
internal static string SerializeXmlByModel(TeaModel obj) { Type type = obj.GetType(); PropertyInfo[] properties = type.GetProperties(); if (obj == null || properties.Length == 0) { return(string.Empty); } PropertyInfo propertyInfo = properties[0]; NameInMapAttribute attribute = propertyInfo.GetCustomAttribute(typeof(NameInMapAttribute)) as NameInMapAttribute; string realName = attribute == null ? propertyInfo.Name : attribute.Name; object rootObj = propertyInfo.GetValue(obj); XElement element = new XElement(realName); GetXmlFactory(rootObj, element); return(element.ToString()); }