using DevExpress.XtraEditors; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Data; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; using ComboBox = System.Windows.Forms.ComboBox; namespace WinformGeneralDeveloperFrame.Commons { public static class ExtensionMethod { public static void SetComboxEditDataSource(this ComboBoxEdit cmb,IEnumerable dataSource,string textFileName,string valueFileName) { foreach (var item in dataSource) { //cmb.Properties.Items.Add(); } } /// /// 显示一般的提示信息 /// /// 提示信息 public static DialogResult ShowTips(this string message) { return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("提示信息"), MessageBoxButtons.OK, MessageBoxIcon.Information); } /// /// 显示警告信息 /// /// 警告信息 public static DialogResult ShowWarning(this string message) { return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("警告信息"), MessageBoxButtons.OK, MessageBoxIcon.Warning); } /// /// 显示错误信息 /// /// 错误信息 public static DialogResult ShowError(this string message) { return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("错误信息"), MessageBoxButtons.OK, MessageBoxIcon.Error); } /// /// 显示询问用户信息,并显示错误标志 /// /// 错误信息 public static DialogResult ShowYesNoAndError(this string message) { return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("错误信息"), MessageBoxButtons.YesNo, MessageBoxIcon.Error); } /// /// 显示询问用户信息,并显示提示标志 /// /// 错误信息 public static DialogResult ShowYesNoAndTips(this string message) { return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("提示信息"), MessageBoxButtons.YesNo, MessageBoxIcon.Information); } /// /// 显示询问用户信息,并显示警告标志 /// /// 警告信息 public static DialogResult ShowYesNoAndWarning(this string message) { return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("警告信息"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning); } /// /// 显示询问用户信息,并显示提示标志 /// /// 错误信息 public static DialogResult ShowYesNoCancelAndTips(this string message) { return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("提示信息"), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); } //public static string language(this string message) //{ // return message; //} public static bool ToBoolean(this string str) { bool result = false; bool.TryParse(str, out result); return result; } public static void SetDataGridViewDoubleBuffered(this DataGridView dataGridView) { Type dgvType = dataGridView.GetType(); PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic); pi.SetValue(dataGridView, true, null); } /// /// 查询扩展方法 /// public static IOrderedQueryable OrderBy(this IQueryable query, string propertyName) { return _OrderBy(query, propertyName, false); } public static IOrderedQueryable OrderByDescending(this IQueryable query, string propertyName) { return _OrderBy(query, propertyName, true); } static IOrderedQueryable _OrderBy(IQueryable query, string propertyName, bool isDesc) { string methodname = (isDesc) ? "OrderByDescendingInternal" : "OrderByInternal"; var memberProp = typeof(T).GetProperty(propertyName); var method = typeof(ExtensionMethod).GetMethod(methodname) .MakeGenericMethod(typeof(T), memberProp.PropertyType); return (IOrderedQueryable)method.Invoke(null, new object[] { query, memberProp }); } public static IOrderedQueryable OrderByInternal(IQueryable query, System.Reflection.PropertyInfo memberProperty) {//public return query.OrderBy(_GetLamba(memberProperty)); } public static IOrderedQueryable OrderByDescendingInternal(IQueryable query, System.Reflection.PropertyInfo memberProperty) {//public return query.OrderByDescending(_GetLamba(memberProperty)); } static Expression> _GetLamba(System.Reflection.PropertyInfo memberProperty) { if (memberProperty.PropertyType != typeof(TProp)) throw new Exception(); var thisArg = Expression.Parameter(typeof(T)); var lamba = Expression.Lambda>(Expression.Property(thisArg, memberProperty), thisArg); return lamba; } /// /// 转换字符串为Int16类型,可以指定默认值 /// /// 字符串内容 /// 默认值 /// public static int ToInt16(this string str, Int16 defaultValue = 0) { str = ConvertHelper.ConvertToDBC(str);//先转换为半角字符串 bool converted = Int16.TryParse(str, out defaultValue); return defaultValue; } public static DateTime ToDateTime(this object rowValue) { DateTime defMinValue = DateTime.Parse("1758-01-01 00:00:00"); DateTime defMaxValue = DateTime.Parse("9999-12-31 23:59:59"); if (rowValue == null || rowValue == DBNull.Value) { return defMinValue;//传入空值,返回预设值 } DateTime d; if (DateTime.TryParse(rowValue.ToString(), out d)) { if (d < defMinValue || d > defMaxValue) return defMinValue;//无效日期,预设返回SQL支持的最小日期 else return d; } else return defMinValue; } public static void ModelDataToControl(this Control con, object data) { Type type = data.GetType(); foreach (PropertyInfo pro in type.GetProperties()) { if (pro.IsDefined(typeof(ModelBindControlAttribute), true)) { Control control = getControl(con, pro.GetCustomAttribute().GetModelName()); if (control != null) { if (control is ComboBoxEdit) { ComboBoxEdit txt = (ComboBoxEdit)control; txt.EditValue = pro.GetValue(data) ; } else if (control is DateEdit) { DateEdit txt = (DateEdit)control; txt.EditValue = pro.GetValue(data); } else if (control is CheckEdit) { CheckEdit txt = (CheckEdit)control; txt.Checked = Boolean.Parse(pro.GetValue(data).ToString()); } else if (control is LookUpEditBase) { LookUpEditBase txt = (LookUpEditBase)control; txt.EditValue = pro.GetValue(data); } else if (control is CheckedComboBoxEdit) { CheckedComboBoxEdit txt = (CheckedComboBoxEdit)control; txt.SetEditValue(pro.GetValue(data)); } else { TextEdit txt = (TextEdit)control; txt.Text = pro.GetValue(data) == null ? "" : pro.GetValue(data).ToString(); } } } } } public static Control getControl(this Control con, string name) { Control[] controls = con.Controls.Find(name, true); if (controls.Length > 0) return (controls[0]); else { return null; } } public static object ControlDataToModel(this Control con,object data) { Type type = data.GetType(); object t = Activator.CreateInstance(type); foreach (PropertyInfo pro in type.GetProperties()) { if (pro.IsDefined(typeof(ModelBindControlAttribute), true)) { Control control = getControl(con, pro.GetCustomAttribute().GetModelName()); if (control != null) { if (control is ComboBoxEdit) { ComboBoxEdit txt = (ComboBoxEdit)control; pro.SetValue(t, txt.EditValue); } else if (control is DateEdit) { DateEdit txt = (DateEdit)control; pro.SetValue(t, txt.DateTime); } else if (control is CheckEdit) { CheckEdit txt = (CheckEdit)control; pro.SetValue(t, txt.Checked); } else if (control is NumericUpDown) { NumericUpDown txt = (NumericUpDown)control; pro.SetValue(t, Convert.ChangeType(txt.Text, pro.PropertyType)); } else if (control is LookUpEditBase) { LookUpEditBase txt = (LookUpEditBase)control; if(!string.IsNullOrEmpty(txt.EditValue.ToString())) pro.SetValue(t, Convert.ChangeType(txt.EditValue, pro.PropertyType)); } else if (control is CheckedComboBoxEdit) { CheckedComboBoxEdit txt = (CheckedComboBoxEdit)control; pro.SetValue(t, Convert.ChangeType(txt.EditValue, pro.PropertyType)); } else { TextEdit txt = (TextEdit)control; if (string.IsNullOrEmpty(txt.Text)) { txt.Text = "0"; } pro.SetValue(t, Convert.ChangeType(txt.Text,pro.PropertyType)); } } } } return t; } public static T RowToModel(this DataRow dr) where T : new() { T model = new T(); foreach (PropertyInfo p in (typeof(T)).GetProperties()) { if (dr[p.Name] is System.DBNull) { continue; } p.SetValue(model, dr[p.Name.ToUpper()], null); } return model; } /// /// list转datatable /// /// /// /// public static DataTable ToDataTable(this IEnumerable collection) { var props = typeof(T).GetProperties(); var dt = new DataTable(); dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray()); if (collection.Count() > 0) { for (int i = 0; i < collection.Count(); i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in props) { object obj = pi.GetValue(collection.ElementAt(i), null); tempList.Add(obj); } object[] array = tempList.ToArray(); dt.LoadDataRow(array, true); } } return dt; } /// /// 获取datatable数据 /// /// /// public static Dictionary> GetDataTableData(this DataTable dt)where T:class,new() { Dictionary> data = new Dictionary>(); List listAdd = new List(); List listEdit = new List(); List listDel = new List(); foreach (DataRow item in dt.Rows) { if (item.RowState == DataRowState.Added ) { T t = item.RowToModel(); if (t != null) { listAdd.Add(t); } }else if (item.RowState == DataRowState.Modified) { T t = item.RowToModel(); if (t != null) { listEdit.Add(t); } } //listDel = dt.GetChanges(DataRowState.Deleted).DataTableToList(); DataTable dtDeleted = dt.GetChanges(DataRowState.Deleted); if (dtDeleted != null) { foreach (DataRow row in dtDeleted.Rows) { T t = new T(); foreach (PropertyInfo fieldInfo in t.GetType().GetProperties()) { fieldInfo.SetValue(t, row[fieldInfo.Name, DataRowVersion.Original], null); } listDel.Add(t); } } } data.Add("Add",listAdd); data.Add("Del",listDel); data.Add("Edit", listEdit); return data; } /// /// 利用反射将DataTable转换为List对象 /// /// DataTable 对象 /// List集合 public static List DataTableToList(this DataTable dt) where T : class, new() { // 定义集合 List ts = new List(); //定义一个临时变量 string tempName = string.Empty; //遍历DataTable中所有的数据行 foreach (DataRow dr in dt.Rows) { T t = new T(); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); //遍历该对象的所有属性 foreach (PropertyInfo pi in propertys) { tempName = pi.Name;//将属性名称赋值给临时变量 //检查DataTable是否包含此列(列名==对象的属性名) if (dt.Columns.Contains(tempName)) { //取值 object value = dr[tempName]; //如果非空,则赋给对象的属性 if (value != DBNull.Value) { pi.SetValue(t, value, null); } } } //对象添加到泛型集合中 ts.Add(t); } return ts; } } }