private Dictionary <string, object> RenderDictioary(ServiceReader reader, Dictionary <string, ParameterInfo> parameters) { Dictionary <string, object> dictionarys = new Dictionary <string, object>(); foreach (string key in reader.Values.AllKeys) { if (parameters.ContainsKey(key)) { var reqValue = reader.Values[key]; Type parameterType = parameters[key].ParameterType; object parameterValue = null; if (string.IsNullOrWhiteSpace(reqValue)) { parameterType = TypeConvertHelper.GetUnderlyingType(parameterType); parameterValue = TypeConvertHelper.ChangeType(parameterType, reqValue); continue; } if (TypeConvertHelper.IsPrimitive(parameterType)) { parameterType = TypeConvertHelper.GetUnderlyingType(parameterType); parameterValue = TypeConvertHelper.ChangeType(parameterType, reqValue); dictionarys.Add(key, parameterValue); } else if (parameterType.IsClass) { parameterValue = JsonConvertHelper.DeserializeObject(reqValue, parameterType); dictionarys.Add(key, parameterValue); } } } return(dictionarys); }
/// <summary> /// 设定对象某个属性的值 /// </summary> /// <param name="obj"></param> /// <param name="propertyName"></param> /// <param name="proValue"></param> /// <param name="ignoreError">是否忽略错误消息</param> public static void SetProperty(object obj, string propertyName, object proValue, bool ignoreError) { Type type = obj.GetType(); PropertyInfo property = type.GetProperty(propertyName); if (!((property != null) && property.CanWrite)) { if (!ignoreError) { throw new Exception(string.Format("The setter of property named '{0}' not found in '{1}'.", propertyName, type)); } } else { try { proValue = TypeConvertHelper.ChangeType(property.PropertyType, proValue); } catch { } object[] args = new object[] { proValue }; type.InvokeMember(propertyName, BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase, null, obj, args); } }