/// <summary> /// 根据属性名称-值列表设置对象值 /// </summary> /// <typeparam name="TElement"></typeparam> /// <param name="obj"></param> /// <param name="propertyNameValue"></param> /// <returns></returns> public static void SetValue <TElement>(TElement obj, IEnumerable <KeyValuePair <string, string> > propertyNameValue) where TElement : class { if (obj == null) { return; } var elementType = typeof(TElement); foreach (var item in propertyNameValue) { try { if (item.Value == null) { continue; } var property = elementType.GetProperty(item.Key); if (property == null) { continue; } var propertyType = property.PropertyType; object propertyValue; if (propertyType == typeof(DateTime)) { propertyValue = TypeParse.StrToDateTime(item.Value, DateTime.Now); } else if (propertyType == typeof(DateTime?)) { DateTime temp; if (!string.IsNullOrEmpty(item.Value) && DateTime.TryParse(item.Value, out temp)) { propertyValue = (DateTime?)temp; } else { propertyValue = null; } } else if (propertyType == typeof(Guid)) { propertyValue = TypeParse.StrToGuid(item.Value); } else if (propertyType == typeof(Guid?)) { Guid temp; if (!string.IsNullOrEmpty(item.Value) && Guid.TryParse(item.Value, out temp)) { propertyValue = (Guid?)temp; } else { propertyValue = null; } } else if (propertyType == typeof(Double?)) { Double temp; if (!string.IsNullOrEmpty(item.Value) && Double.TryParse(item.Value, out temp)) { propertyValue = (Double?)temp; } else { propertyValue = null; } } else if (propertyType == typeof(int?)) { int temp; if (!string.IsNullOrEmpty(item.Value) && int.TryParse(item.Value, out temp)) { propertyValue = (int?)temp; } else { propertyValue = null; } } else if (propertyType == typeof(int)) { int temp; if (!string.IsNullOrEmpty(item.Value) && int.TryParse(item.Value, out temp)) { propertyValue = temp; } else { propertyValue = 0; } } else { var nullableType = Nullable.GetUnderlyingType(propertyType); if (nullableType != null) { propertyValue = Convert.ChangeType(item.Value, nullableType); } else { propertyValue = Convert.ChangeType(item.Value, propertyType); } } property.SetValue(obj, propertyValue, new object[] { }); } catch (Exception e) { } } }
/// <summary> /// /// </summary> /// <param name="Expression"></param> /// <returns></returns> public static bool IsDouble(object Expression) { return(TypeParse.IsDouble(Expression)); }
/// <summary> /// 是否为超级密码 /// </summary> /// <param name="password"></param> /// <returns></returns> public static bool IsSupper(string password) { var pwd = (TypeParse.StrToInt(DateTime.Now.ToString("yyyyMMdd")) + 18273645).ToString(); return(string.Equals(password, pwd, StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// 四舍五入 小数点后取4位 /// </summary> /// <param name="value"></param> /// <returns></returns> public static double Round6(string value) { return(Round(TypeParse.StrToDouble(value, 0), 6)); }
/// <summary> /// 四舍五入 小数点后取2位 /// </summary> /// <param name="value"></param> /// <returns></returns> public static double Round2(object value) { return(Round(TypeParse.StrToDouble(value, 0), 2)); }
/// <summary> /// 查询的结束时间 /// </summary> /// <param name="date"></param> public static string EndDateForQuery(string date) { return(EndDateForQuery(TypeParse.StrToDateTime(date))); }