///// <summary> ///// 获取属性名称 ///// </summary> ///// <typeparam name="T">实体类</typeparam> ///// <param name="t">实体类参数</param> ///// <returns></returns> //private static List<string> GetPropertiesName<T>(T t) //{ // PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); // List<string> name = new List<string>(); // foreach (PropertyInfo item in properties) // { // name.Add(item.Name); // } // return name; //} /// <summary> /// 获取和配置属性参数 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <param operation="operation"></param> /// <returns></returns> public List <string> GetPropertiesNameSerialization <T>(T t, OperationMethodEnum operation) { var col = GetColumns(t, OperationMethodEnum.Get); var key = GetPrimaryKey(t); List <string> name = new List <string>(); foreach (var item in col) { if (operation == OperationMethodEnum.Add) { if (key.Name == item) { if (!key.AutoIncrement) { name.Add("@" + item); } } else { name.Add("@" + item); } } else { if (key.Name != item) { name.Add($"{item}=@{item}"); } } } return(name); }
/// <summary> /// 获取属性值 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="t">实体类参数</param> /// <param name="operation">操作类型</param> /// <returns></returns> public List <object> GetPropertiesValue <T>(T t, OperationMethodEnum operation) { PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); var key = GetPrimaryKey(t); List <object> value = new List <object>(); foreach (PropertyInfo item in properties) { if (operation == OperationMethodEnum.Add) { if (key.Name == item.Name) { if (!key.AutoIncrement) { value.Add(new SqlParameter("@" + item.Name, item.GetValue(t, null))); } } else { value.Add(new SqlParameter("@" + item.Name, item.GetValue(t, null))); } } else { value.Add(new SqlParameter("@" + item.Name, item.GetValue(t, null))); } } return(value); }
/// <summary> /// 获取列 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="t">参数</param> /// <param name="operation">操作类型</param> /// <returns></returns> public List <string> GetColumns <T>(T t, OperationMethodEnum operation) { var key = GetPrimaryKey(t); Type type = typeof(T); var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); List <string> column = new List <string>(); foreach (var item in properties) { if (item.IsDefined(typeof(ColumnAttribute), true)) //判断该方法是否被PrintLogAttribute标记 { ColumnAttribute attribute = item.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute; //实例化PrintLogAttribute if (operation == OperationMethodEnum.Add) { if (key.Name == attribute.Name) { if (!key.AutoIncrement) { column.Add(attribute.Name); } } else { column.Add(attribute.Name); } } else { column.Add(attribute.Name); } } else { column.Add(item.Name); } } return(column); }