/// <summary>读取</summary> /// <param name="reader"></param> /// <param name="value"></param> public static void ReadXml(XmlReader reader, Object value) { foreach (var item in GetProperties(value.GetType())) { if (!item.Property.CanRead) { continue; } if (AttributeX.GetCustomAttribute <XmlIgnoreAttribute>(item.Member, false) != null) { continue; } var v = reader.GetAttribute(item.Name); if (String.IsNullOrEmpty(v)) { continue; } if (item.Type == typeof(String[])) { var ss = v.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); item.SetValue(value, ss); } else { item.SetValue(value, TypeX.ChangeType(v, item.Type)); } } //reader.Skip(); }
protected T GetData <T>(string name) { object data = this.GetData(name); if (data == null) { return(default(T)); } return((T)TypeX.ChangeType(data, typeof(T))); }
private void SetEntityItem(IEntity entity, FieldItem field, Object value) { // 先转为目标类型 value = TypeX.ChangeType(value, field.Type); // 如果是字符串,并且为空,则让它等于实体里面的值,避免影响脏数据 if (field.Type == typeof(String) && String.IsNullOrEmpty((String)value) && String.IsNullOrEmpty((String)entity[field.Name])) { value = entity[field.Name]; } entity.SetItem(field.Name, value); }
/// <summary>获取数据,主要处理类型转换</summary> /// <typeparam name="T"></typeparam> /// <param name="name"></param> /// <returns></returns> protected T GetData <T>(String name) { Object obj = GetData(name); if (obj == null) { return(default(T)); } return((T)TypeX.ChangeType(obj, typeof(T))); }
/// <summary>尝试获取指定名称的设置项</summary> /// <param name="name"></param> /// <param name="type"></param> /// <param name="value"></param> /// <returns></returns> public static Boolean TryGetConfig(String name, Type type, out Object value) { value = null; try { NameValueCollection nvs = AppSettings; if (nvs == null || nvs.Count < 1) { return(false); } String str = nvs[name]; if (String.IsNullOrEmpty(str)) { return(false); } TypeCode code = Type.GetTypeCode(type); if (code == TypeCode.String) { value = str; } else if (code == TypeCode.Int32) { value = Convert.ToInt32(str); } else if (code == TypeCode.Boolean) { Boolean b = false; if (str == "1" || str.EqualIgnoreCase(Boolean.TrueString)) { value = true; } else if (str == "0" || str.EqualIgnoreCase(Boolean.FalseString)) { value = false; } else if (Boolean.TryParse(str.ToLower(), out b)) { value = b; } } else { value = TypeX.ChangeType(str, type); } return(true); } catch (ConfigurationErrorsException) { return(false); } }
/// <summary>指定键是否为空。一般业务系统设计不允许主键为空,包括自增的0和字符串的空</summary> /// <param name="key">键值</param> /// <param name="type">类型</param> /// <returns></returns> public static Boolean IsNullKey(Object key, Type type) { if (key == null) { return(true); } if (type == null) { type = key.GetType(); } key = TypeX.ChangeType(key, type); //由于key的实际类型是由类型推倒而来,所以必须根据实际传入的参数类型分别进行装箱操作 //如果不根据类型分别进行会导致类型转换失败抛出异常 switch (Type.GetTypeCode(type)) { case TypeCode.Int16: return(((Int16)key) <= 0); case TypeCode.Int32: return(((Int32)key) <= 0); case TypeCode.Int64: return(((Int64)key) <= 0); case TypeCode.UInt16: return(((UInt16)key) <= 0); case TypeCode.UInt32: return(((UInt32)key) <= 0); case TypeCode.UInt64: return(((UInt64)key) <= 0); case TypeCode.String: return(String.IsNullOrEmpty((String)key)); default: break; } if (type == typeof(Guid)) { return(((Guid)key) == Guid.Empty); } if (type == typeof(Byte[])) { return(((Byte[])key).Length <= 0); } return(false); }
/// <summary>获取所有实体中指定项的值</summary> /// <typeparam name="TResult">指定项的类型</typeparam> /// <param name="name"></param> /// <returns></returns> public List <TResult> GetItem <TResult>(String name) { if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } //if (Count < 1) return null; List <TResult> list = new List <TResult>(); if (Count < 1) { return(list); } Type type = typeof(TResult); foreach (T item in this) { if (item == null) { continue; } //Object obj = item[name]; //if (obj is TResult) // list.Add((TResult)item[name]); //else // list.Add((TResult)TypeX.ChangeType(obj, type)); //list.Add(TypeX.ChangeType<TResult>(item[name])); // 避免集合插入了重复项 TResult obj = TypeX.ChangeType <TResult>(item[name]); if (!list.Contains(obj)) { list.Add(obj); } } return(list); }
/// <summary>设置字段值,该方法影响脏数据。</summary> /// <param name="name">字段名</param> /// <param name="value">值</param> /// <returns>返回是否成功设置了数据</returns> public Boolean SetItem(String name, Object value) { var fact = EntityFactory.CreateOperate(GetType()); FieldItem fi = fact.Table.FindByName(name); // 确保数据类型一致 if (fi != null) { value = TypeX.ChangeType(value, fi.Type); } var b = OnPropertyChanging(name, value); if (b) { // OnPropertyChanging中根据新旧值是否相同来影响脏数据 // SetItem作为必定影响脏数据的代替者 this[name] = value; Dirtys[name] = true; } return(b); }
/// <summary>取得指定名称的设置项,并分割为指定类型数组</summary> /// <typeparam name="T"></typeparam> /// <param name="name"></param> /// <param name="split"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static T[] GetConfigSplit <T>(String name, String split, T[] defaultValue) { try { NameValueCollection nvs = AppSettings; if (nvs == null || nvs.Count < 1) { return(defaultValue); } String str = GetConfig <String>(name); if (String.IsNullOrEmpty(str)) { return(defaultValue); } String[] sps = String.IsNullOrEmpty(split) ? new String[] { ",", ";" } : new String[] { split }; String[] ss = str.Split(sps, StringSplitOptions.RemoveEmptyEntries); if (ss == null || ss.Length < 1) { return(defaultValue); } T[] arr = new T[ss.Length]; for (int i = 0; i < ss.Length; i++) { str = ss[i].Trim(); if (String.IsNullOrEmpty(str)) { continue; } arr[i] = TypeX.ChangeType <T>(str); } return(arr); } catch (ConfigurationErrorsException) { return(defaultValue); } }
/// <summary>格式化数据为SQL数据</summary> /// <param name="field"></param> /// <param name="value"></param> /// <returns></returns> public virtual String FormatValue(IDataColumn field, Object value) { Boolean isNullable = true; Type type = null; if (field != null) { type = field.DataType; isNullable = field.Nullable; } else if (value != null) { type = value.GetType(); } TypeCode code = Type.GetTypeCode(type); if (code == TypeCode.String) { if (value == null) { return(isNullable ? "null" : "''"); } //!!! 为SQL格式化数值时,如果字符串是Empty,将不再格式化为null //if (String.IsNullOrEmpty(value.ToString()) && isNullable) return "null"; return("'" + value.ToString().Replace("'", "''") + "'"); } else if (code == TypeCode.DateTime) { if (value == null) { return(isNullable ? "null" : "''"); } DateTime dt = Convert.ToDateTime(value); if (dt < DateTimeMin || dt > DateTime.MaxValue) { return(isNullable ? "null" : "''"); } if ((dt == DateTime.MinValue || dt == DateTimeMin) && isNullable) { return("null"); } return(FormatDateTime(dt)); } else if (code == TypeCode.Boolean) { if (value == null) { return(isNullable ? "null" : ""); } return(Convert.ToBoolean(value) ? "1" : "0"); } else if (type == typeof(Byte[])) { Byte[] bts = (Byte[])value; if (bts == null || bts.Length < 1) { return(isNullable ? "null" : "0x0"); } return("0x" + BitConverter.ToString(bts).Replace("-", null)); } else if (field.DataType == typeof(Guid)) { if (value == null) { return(isNullable ? "null" : "''"); } return(String.Format("'{0}'", value)); } else { if (value == null) { return(isNullable ? "null" : ""); } // 转为目标类型,比如枚举转为数字 value = TypeX.ChangeType(value, type); if (value == null) { return(isNullable ? "null" : ""); } return(value.ToString()); } }