static void TestListDictionary() { string abc = "aaaaaaaaaabc"; List <string> s = "2,2,3,2,4,3,1,2".Split(',').OrderBy(p => p).Distinct().ToList(); Dictionary <string, object> dic = new Dictionary <string, object>(); dic.Add("1", "a"); dic.Add("2", s); dic.Add("3", "a"); dic.Add("4", "a"); string json = abc; MDataTable dt = MDataTable.CreateFrom(dic); Entity e = new Entity(); e.ID = 1111; e.Name = "b"; object o = e; System.Object ooo = new object(); ooo = e; MDataRow row = MDataRow.CreateFrom(e); ObservableCollection <string> sa = new ObservableCollection <string>(s); Console.Read(); }
private void btnFill_Click(object sender, EventArgs e) { UsersBean ub = DBFast.Find <UsersBean>(txtUserID); MDataRow.CreateFrom(ub).SetToAll(this); OutMsg(); }
private void btnInsert_Click(object sender, EventArgs e) { MDataRow row = MDataRow.CreateFrom(new UsersBean()); row.LoadFrom(false, this); DBFast.Insert <UsersBean>(row.ToEntity <UsersBean>(), InsertOp.ID, chbInsertID.Checked); LoadData(); OutMsg(); }
private void btnUpdate_Click(object sender, EventArgs e) { MDataRow row = MDataRow.CreateFrom(new UsersBean()); row.LoadFrom(false, this); DBFast.Update <UsersBean>(row.ToEntity <UsersBean>()); LoadData(); OutMsg(); }
private void btnFill_Click(object sender, EventArgs e) { using (UsersBean ub = new UsersBean()) { if (ub.Fill(txtUserID)) { MDataRow.CreateFrom(ub).SetToAll(this); } } OutMsg(); }
private void btnUpdate_Click(object sender, EventArgs e) { using (UsersBean ub = new UsersBean()) { MDataRow row = MDataRow.CreateFrom(ub); row.LoadFrom(false, this); row.SetToEntity(ub); ub.Update(); } LoadData(); OutMsg(); }
/// <summary> /// 类型转换(精准强大) /// </summary> /// <param name="value">值处理</param> /// <param name="t">类型</param> /// <returns></returns> public static object ChangeType(object value, Type t) { if (t == null) { return(null); } string strValue = Convert.ToString(value); if (t.IsGenericType && t.Name.StartsWith("Nullable")) { t = Nullable.GetUnderlyingType(t); if (strValue == "") { return(null); } } if (t.Name == "String") { return(strValue); } if (strValue == "") { return(Activator.CreateInstance(t)); } else if (t.IsValueType) { if (t.Name == "Guid") { return(new Guid(strValue)); } else if (t.Name.StartsWith("Int") && strValue.IndexOf('.') > -1) { strValue = strValue.Split('.')[0]; } return(Convert.ChangeType(strValue, t)); } else { switch (GetSystemType(ref t)) { case SysType.Custom: return(MDataRow.CreateFrom(strValue).ToEntity(t)); case SysType.Generic: return(MDataTable.CreateFrom(strValue).ToList(t)); } return(Convert.ChangeType(value, t)); } }
private void btnInsert_Click(object sender, EventArgs e) { using (UsersBean ub = new UsersBean()) { MDataRow row = MDataRow.CreateFrom(ub); row.LoadFrom(false, this); row.SetToEntity(ub); if (ub.Insert(InsertOp.ID, chbInsertID.Checked)) { row.SetToAll(this); } } LoadData(); OutMsg(); }
/// <summary> /// 类型转换(精准强大) /// </summary> /// <param name="value">值处理</param> /// <param name="t">类型</param> /// <returns></returns> public static object ChangeType(object value, Type t) { if (t == null) { return(null); } if (t.FullName == "System.Type") { return((Type)value); } string strValue = Convert.ToString(value); if (t.IsGenericType && t.Name.StartsWith("Nullable")) { t = Nullable.GetUnderlyingType(t); if (strValue == "") { return(null); } } if (t.Name == "String") { if (value is byte[]) { return(Convert.ToBase64String((byte[])value)); } return(strValue); } if (t.FullName == "System.Text.StringBuilder") { return(value as StringBuilder); } if (t.FullName == "System.Text.Encoding") { return(value as Encoding); } if (strValue == "") { return(Activator.CreateInstance(t)); } else if (t.IsValueType) { if (t.Name == "DateTime") { switch (strValue.ToLower().TrimEnd(')', '(')) { case "now": case "getdate": case "current_timestamp": return(DateTime.Now); } if (DateTime.Parse(strValue) == DateTime.MinValue) { return((DateTime)SqlDateTime.MinValue); } return(Convert.ChangeType(value, t));//这里用value,避免丢失毫秒 } if (t.Name == "Guid") { if (strValue == SqlValue.Guid || strValue.StartsWith("newid")) { return(Guid.NewGuid()); } return(new Guid(strValue)); } else if (t.Name.StartsWith("Int")) { switch (strValue.ToLower()) { case "true": return(1); case "false": return(0); } if (strValue.IndexOf('.') > -1) { strValue = strValue.Split('.')[0]; } else if (value.GetType().IsEnum) { return((int)value); } } else if (t.Name == "Double" || t.Name == "Single") { switch (strValue.ToLower()) { case "infinity": case "正无穷大": return(double.PositiveInfinity); case "-infinity": case "负无穷大": return(double.NegativeInfinity); } } else if (t.Name == "Boolean") { switch (strValue.ToLower()) { case "yes": case "true": case "1": case "on": case "是": return(true); case "no": case "false": case "0": case "": case "否": default: return(false); } } else if (t.IsEnum) { return(Enum.Parse(t, strValue, true)); } return(Convert.ChangeType(strValue, t)); } else { Type valueType = value.GetType(); //if(valueType.IsEnum && t.is) if (valueType.FullName != t.FullName) { switch (ReflectTool.GetSystemType(ref t)) { case SysType.Custom: return(MDataRow.CreateFrom(strValue).ToEntity(t)); case SysType.Generic: if (t.Name.StartsWith("List")) { return(MDataTable.CreateFrom(strValue).ToList(t)); } break; case SysType.Array: if (t.Name == "Byte[]") { if (valueType.Name == "String") { return(Convert.FromBase64String(strValue)); } using (MemoryStream ms = new MemoryStream()) { new BinaryFormatter().Serialize(ms, value); return(ms.ToArray()); } } break; } } return(Convert.ChangeType(value, t)); } }
/// <summary> /// 类型转换(精准强大) /// </summary> /// <param name="value">值处理</param> /// <param name="t">类型</param> /// <returns></returns> public static object ChangeType(object value, Type t) { if (t == null) { return(null); } if (t.FullName == "System.Type") { return((Type)value); } string strValue = Convert.ToString(value); if (t.IsGenericType && t.Name.StartsWith("Nullable")) { t = Nullable.GetUnderlyingType(t); if (strValue == "") { return(null); } } if (t.Name == "String") { return(strValue); } if (t.FullName == "System.Text.StringBuilder") { return(value as StringBuilder); } if (t.FullName == "System.Text.Encoding") { return(value as Encoding); } if (strValue == "") { return(Activator.CreateInstance(t)); } else if (t.IsValueType) { if (t.Name == "DateTime") { return(Convert.ChangeType(value, t));//这里用value,避免丢失毫秒 } if (t.Name == "Guid") { return(new Guid(strValue)); } else if (t.Name.StartsWith("Int") && strValue.IndexOf('.') > -1) { strValue = strValue.Split('.')[0]; } else if (t.Name == "Boolean") { switch (strValue.ToLower()) { case "yes": case "true": case "1": case "on": case "是": return(true); case "no": case "false": case "0": case "": case "否": default: return(false); } } return(Convert.ChangeType(strValue, t)); } else { if (strValue != t.FullName) { switch (GetSystemType(ref t)) { case SysType.Custom: return(MDataRow.CreateFrom(strValue).ToEntity(t)); case SysType.Generic: if (t.Name.StartsWith("List")) { return(MDataTable.CreateFrom(strValue).ToList(t)); } break; case SysType.Array: if (t.Name == "Byte[]" && value.GetType().Name != t.Name) { using (MemoryStream ms = new MemoryStream()) { new BinaryFormatter().Serialize(ms, value); return(ms.ToArray()); } } break; } } return(Convert.ChangeType(value, t)); } }
/// <summary> /// 类型转换(精准强大) /// </summary> /// <param name="value">值处理</param> /// <param name="t">类型</param> /// <returns></returns> public static object ChangeType(object value, Type t) { if (t == null) { return(null); } string strValue = Convert.ToString(value); if (t.IsGenericType && t.Name.StartsWith("Nullable")) { t = Nullable.GetUnderlyingType(t); if (strValue == "") { return(null); } } if (t.Name == "String") { return(strValue); } if (strValue == "") { return(Activator.CreateInstance(t)); } else if (t.IsValueType) { if (t.Name == "Guid") { return(new Guid(strValue)); } else if (t.Name.StartsWith("Int") && strValue.IndexOf('.') > -1) { strValue = strValue.Split('.')[0]; } return(Convert.ChangeType(strValue, t)); } else { switch (GetSystemType(ref t)) { case SysType.Custom: return(MDataRow.CreateFrom(strValue).ToEntity(t)); case SysType.Generic: if (t.Name.StartsWith("List")) { return(MDataTable.CreateFrom(strValue).ToList(t)); } break; case SysType.Array: if (t.Name == "Byte[]" && value.GetType().Name != t.Name) { using (MemoryStream ms = new MemoryStream()) { new BinaryFormatter().Serialize(ms, value); return(ms.ToArray()); } } break; } return(Convert.ChangeType(value, t)); } }
/// <summary> /// 类型转换(精准强大) /// </summary> /// <param name="value">值处理</param> /// <param name="t">类型</param> /// <returns></returns> public static object ChangeType(object value, Type t) { if (t == null) { return(null); } string strValue = Convert.ToString(value); if (t.IsEnum) { if (strValue != "") { if (Enum.IsDefined(t, strValue)) { return(Enum.Parse(t, strValue)); } int v = 0; if (int.TryParse(strValue, out v)) { object v1 = Enum.Parse(t, strValue); if (v1.ToString() != strValue) { return(v1); } } string[] names = Enum.GetNames(t); string lower = strValue.ToLower(); foreach (string name in names) { if (name.ToLower() == lower) { return(Enum.Parse(t, name)); } } } //取第一个值。 string firstKey = Enum.GetName(t, -1); if (!string.IsNullOrEmpty(firstKey)) { return(Enum.Parse(t, firstKey)); } return(Enum.Parse(t, Enum.GetNames(t)[0])); } if (value == null) { return(null); } if (t.FullName == "System.Object") { return(value); } if (t.FullName == "System.Type") { return((Type)value); } if (t.FullName == "System.IO.Stream" && value is HttpPostedFile) { return(((HttpPostedFile)value).InputStream); } if (t.IsGenericType && t.Name.StartsWith("Nullable")) { t = Nullable.GetUnderlyingType(t); if (strValue == "") { return(null); } } if (t.Name == "String") { if (value is byte[]) { return(Convert.ToBase64String((byte[])value)); } return(strValue); } if (t.FullName == "System.Text.StringBuilder") { return(value as StringBuilder); } if (t.FullName == "System.Text.Encoding") { return(value as Encoding); } if (strValue == "") { if (t.Name.EndsWith("[]")) { return(null); } return(Activator.CreateInstance(t)); } else if (t.IsValueType) { if (t.Name == "DateTime") { switch (strValue.ToLower().TrimEnd(')', '(')) { case "now": case "getdate": case "current_timestamp": return(DateTime.Now); } if (DateTime.Parse(strValue) == DateTime.MinValue) { return((DateTime)SqlDateTime.MinValue); } return(Convert.ChangeType(value, t));//这里用value,避免丢失毫秒 } else if (t.Name == "Guid") { if (strValue == SqlValue.Guid || strValue.StartsWith("newid")) { return(Guid.NewGuid()); } return(new Guid(strValue)); } else { switch (strValue.ToLower()) { case "yes": case "true": case "1": case "on": case "是": if (t.Name == "Boolean") { return(true); } else { strValue = "1"; } break; case "no": case "false": case "0": case "": case "否": if (t.Name == "Boolean") { return(false); } else { strValue = "0"; } break; case "infinity": case "正无穷大": if (t.Name == "Double" || t.Name == "Single") { return(double.PositiveInfinity); } break; case "-infinity": case "负无穷大": if (t.Name == "Double" || t.Name == "Single") { return(double.NegativeInfinity); } break; default: if (t.Name == "Boolean") { return(false); } break; } if (t.Name.StartsWith("Int") || t.Name == "Byte") { if (strValue.IndexOf('.') > -1)//11.22 { strValue = strValue.Split('.')[0]; } else if (value.GetType().IsEnum) { return((int)value); } } } return(Convert.ChangeType(strValue, t)); } else { Type valueType = value.GetType(); //if(valueType.IsEnum && t.is) if (valueType.FullName != t.FullName) { switch (ReflectTool.GetSystemType(ref t)) { case SysType.Custom: return(MDataRow.CreateFrom(strValue).ToEntity(t)); case SysType.Generic: if (t.Name.StartsWith("List")) { return(MDataTable.CreateFrom(strValue).ToList(t)); } break; case SysType.Array: if (t.Name == "Byte[]") { if (valueType.Name == "String") { return(Convert.FromBase64String(strValue)); } using (MemoryStream ms = new MemoryStream()) { new BinaryFormatter().Serialize(ms, value); return(ms.ToArray()); } } break; } } return(Convert.ChangeType(value, t)); } }
public override void GetData(object target, System.IO.Stream outgoingData) { MDataTable dt = null; #region 类型判断 if (target is MDataTable) { dt = target as MDataTable; } else if (target is MDataRow) { dt = ((MDataRow)target).ToTable(); } else if (target is MDataColumn) { dt = ((MDataColumn)target).ToTable(); } else if (target is MDataRowCollection) { dt = target as MDataRowCollection; } else if (target is DataRow) { MDataRow row = target as DataRow; dt = row.ToTable(); } else if (target is DataColumnCollection) { MDataColumn mdc = target as DataColumnCollection; dt = mdc.ToTable(); } else if (target is DataRowCollection) { MDataRowCollection rows = target as DataRowCollection; dt = rows; } else if (target is NameObjectCollectionBase) { dt = MDataTable.CreateFrom(target as NameObjectCollectionBase); } else if (target is IEnumerable) { dt = MDataTable.CreateFrom(target as IEnumerable); } else { dt = MDataTable.CreateFrom(target); if (dt == null) { MDataRow row = MDataRow.CreateFrom(target); if (row != null) { dt = row.ToTable(); } } } #endregion dt = Format(dt); if (dt != null) { base.GetData(dt.ToDataTable(), outgoingData); } else { base.GetData(new DataTable("Empty Table"), outgoingData); } }
private static CacheManage _AutoCache = CacheManage.Instance; //有可能使用MemCache操作 internal static bool GetCache(AopEnum action, AopInfo aopInfo) //Begin { switch (action) { case AopEnum.ExeNonQuery: case AopEnum.Insert: case AopEnum.Update: case AopEnum.Delete: return(false); } if (!IsCanOperateCache(action, aopInfo)) { return(false); } string baseKey = GetBaseKey(aopInfo); //查看是否通知我移除 string key = GetKey(action, aopInfo, baseKey); object obj = _AutoCache.Get(key); switch (action) { case AopEnum.ExeMDataTableList: if (obj != null) { List <MDataTable> list = new List <MDataTable>(); if (_AutoCache.CacheType == CacheType.LocalCache) { List <MDataTable> listObj = obj as List <MDataTable>; foreach (MDataTable table in listObj) { list.Add(table.Clone()); } } else { Dictionary <string, string> jd = JsonHelper.Split(obj.ToString()); if (jd != null && jd.Count > 0) { foreach (KeyValuePair <string, string> item in jd) { list.Add(MDataTable.CreateFrom(item.Value, null, EscapeOp.Encode)); } } } aopInfo.TableList = list; } break; case AopEnum.Select: case AopEnum.ExeMDataTable: if (obj != null) { if (_AutoCache.CacheType == CacheType.LocalCache) { aopInfo.Table = (obj as MDataTable).Clone(); } else { aopInfo.Table = MDataTable.CreateFrom(obj.ToString(), null, EscapeOp.Encode); } } break; case AopEnum.ExeList: case AopEnum.SelectList: if (obj != null) { aopInfo.ExeResult = obj; } break; case AopEnum.ExeScalar: if (obj != null) { aopInfo.ExeResult = obj; } break; case AopEnum.Fill: if (obj != null) { MDataRow row; if (_AutoCache.CacheType == CacheType.LocalCache) { row = (obj as MDataRow).Clone(); } else { row = MDataRow.CreateFrom(obj); } aopInfo.Row = row; aopInfo.IsSuccess = true; } break; case AopEnum.GetCount: if (obj != null) { aopInfo.RowCount = int.Parse(obj.ToString()); } break; case AopEnum.Exists: if (obj != null) { aopInfo.ExeResult = obj; } break; } baseKey = key = null; return(obj != null); }