从键盘输入一个类的完整包名和类名,利用反射机制,输出该类的父类名、pubic类型的属性名

默认分类 未结 1 2011
_P_erhaps
_P_erhaps 2023-03-21 17:44
1条回答
  • 2023-03-21 18:02

    class MySqlHelper { private string ConnString; public MySqlHelper(string connectionString) { ConnString = connectionString; } public IList RunMySqlSelect4ReturnList(string strCommand) where T : new() { MySqlCommand mySqlCommand = new MySqlCommand(); mySqlCommand.CommandText = strCommand.ToString(); IList ilPropertyInfo = typeof(T).GetProperties().ToList(); //p[0].Name =travel_id; IList ilResult = new List(); 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(string strCommand) where T : new() { MySqlCommand mySqlCommand = new MySqlCommand(); mySqlCommand.CommandText = strCommand.ToString(); IList 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 RunMySqlSelect4ReturnList(DataTable dts) where T : new() { IList ilPropertyInfo = typeof(T).GetProperties().ToList(); //p[0].Name =travel_id; IList ilResult = new List(); 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(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使用反射机制智能推算数据类型以及属性名称

    标签:

    java程序:

    importjava.lang.reflect.Field;importjava.lang.reflect.Modifier;publicclassMain{publicstaticvoidmain(String[]args){StringclassName="java.lang.Math";//类名(含包名)try{Classclazz=Class.forName(className);Field[]fields=clazz.getDeclaredFields();StringsuperClassName;//父类名称intmod;Stringpriv;//修饰符StringfieldName;//属性名称StringfieldType;//属性的数据类型superClassName=clazz.getSuperclass().toString();System.out.println(superClassName.substring(superClassName.indexOf("")+1));for(Fieldfield:fields){mod=field.getModifiers();priv=Modifier.toString(mod);if(priv.indexOf("public")>=0){fieldName=field.getName();fieldType=field.getType().toString();System.out.println(priv+""+fieldType+""+className+"."+fieldName);}}}catch(ClassNotFoundExceptione){System.out.print("exception");}catch(Exceptione){System.out.print("exception");}}}

    运行测试:

    java.lang.Objectpublicstaticfinaldoublejava.lang.Math.Epublicstaticfinaldoublejava.lang.Math.PI

    0 讨论(0)
提交回复