MysqlHelper使用反射机制智能推算数据类型以及属性名称
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MysqlHelper使用反射机制智能推算数据类型以及属性名称,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3975字,纯文字阅读大概需要6分钟。
内容图文
class MySqlHelper { private string ConnString; public MySqlHelper(string connectionString) { ConnString = connectionString; } public IList<T> RunMySqlSelect4ReturnList<T>(string strCommand) where T : new() { MySqlCommand mySqlCommand = new MySqlCommand(); mySqlCommand.CommandText = strCommand.ToString(); IList<PropertyInfo> ilPropertyInfo = typeof(T).GetProperties().ToList(); //p[0].Name =travel_id; IList<T> ilResult = new List<T>(); DataTable dt = new DataTable(); using (MySqlConnection conn = new MySqlConnection(this.ConnString)) { conn.Open(); using (MySqlCommand cmd = mySqlCommand) { cmd.Connection = conn; using (MySqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { dt.Load(rdr); } } } foreach (DataRow dr in dt.Rows) { T tItem = new T(); foreach (var v in ilPropertyInfo) { if (dt.Columns[v.Name] == null) continue; ConvertionExtensions.SetValue(tItem, v.Name, dr[v.Name]); } ilResult.Add(tItem); } return ilResult; } public T getSinggleObj<T>(string strCommand) where T : new() { MySqlCommand mySqlCommand = new MySqlCommand(); mySqlCommand.CommandText = strCommand.ToString(); IList<PropertyInfo> ilPropertyInfo = typeof(T).GetProperties().ToList(); //p[0].Name =travel_id; T ilResult = new T(); DataTable dt = new DataTable(); using (MySqlConnection conn = new MySqlConnection(this.ConnString)) { conn.Open(); using (MySqlCommand cmd = mySqlCommand) { cmd.Connection = conn; using (MySqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { dt.Load(rdr); } } } if (dt.Rows.Count <= 0) { return default(T); } else { foreach (DataRow dr in dt.Rows) { T tItem = new T(); foreach (var v in ilPropertyInfo) { if (dt.Columns[v.Name] == null) continue; ConvertionExtensions.SetValue(tItem, v.Name, dr[v.Name]); } ilResult = tItem; } } return ilResult; } public IList<T> RunMySqlSelect4ReturnList<T>(DataTable dts) where T : new() { IList<PropertyInfo> ilPropertyInfo = typeof(T).GetProperties().ToList(); //p[0].Name =travel_id; IList<T> ilResult = new List<T>(); DataTable dt = dts; foreach (DataRow dr in dt.Rows) { T tItem = new T(); foreach (var v in ilPropertyInfo) { if (dt.Columns[v.Name] == null) continue; ConvertionExtensions.SetValue(tItem, v.Name, dr[v.Name]); } ilResult.Add(tItem); } return ilResult; } } public static class ConvertionExtensions { public static T ConvertTo<T>(this IConvertible convertibleValue) { var t = typeof(T); if (null == convertibleValue) { return default(T); } if (!typeof(T).IsGenericType) { return (T)Convert.ChangeType(convertibleValue, typeof(T)); } else { Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(Nullable<>)) { return (T)Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(typeof(T))); } } throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", convertibleValue.GetType().FullName, typeof(T).FullName)); } public static void SetValue(object inputObject, string propertyName, object propertyVal) { //find out the type Type type = inputObject.GetType(); //get the property information based on the type System.Reflection.PropertyInfo propertyInfo = type.GetProperty(propertyName); //find the property type Type propertyType = propertyInfo.PropertyType; //Convert.ChangeType does not handle conversion to nullable types //if the property type is nullable, we need to get the underlying type of the property var targetType = IsNullableType(propertyInfo.PropertyType) ? Nullable.GetUnderlyingType(propertyInfo.PropertyType) : propertyInfo.PropertyType; //Returns an System.Object with the specified System.Type and whose value is //equivalent to the specified object. propertyVal = Convert.ChangeType(propertyVal, targetType); //Set the value of the property propertyInfo.SetValue(inputObject, propertyVal, null); } private static bool IsNullableType(Type type) { return type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)); } }
MysqlHelper使用反射机制智能推算数据类型以及属性名称
标签:
本文系统来源:http://www.cnblogs.com/xuhongfei/p/4528695.html
内容总结
以上是互联网集市为您收集整理的MysqlHelper使用反射机制智能推算数据类型以及属性名称全部内容,希望文章能够帮你解决MysqlHelper使用反射机制智能推算数据类型以及属性名称所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。