什么叫做反射,反射在编程中起什么作用?

默认分类 未结 1 684
0天使折翼0
0天使折翼0 2023-03-21 17:27
相关标签:
1条回答
  • 2023-03-21 18:06

    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使用反射机制智能推算数据类型以及属性名称

    标签:

    [百度百科里就有你要的资料,学会搜索,以后这10分就能省下了]反射<编程>程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。反射通常具有以下用途: 1.使用 Assembly 定义和加载程序集,加载在程序集清单中列出的模块,以及从此程序集中查找类型并创建该类型的实例。 2.使用 Mole 了解如下的类似信息:包含模块的程序集以及模块中的类等。您还可以获取在模块上定义的所有全局方法或其他特定的非全局方法。 3.使用 ConstructorInfo 了解如下的类似信息:构造函数的名称、参数、访问修饰符(如 public 或 private)和实现详细信息(如 abstract 或 virtual)等。4.使用 Type 的 GetConstructors 或 GetConstructor 方法来调用特定的构造函数。 5.使用 MethodInfo 来了解如下的类似信息:方法的名称、返回类型、参数、访问修饰符(如 public 或 private)和实现详细信息(如 abstract 或 virtual)等。使用 Type 的 GetMethods 或 GetMethod 方法来调用特定的方法。 6.使用 FieldInfo 来了解如下的类似信息:字段的名称、访问修饰符(如 public 或 private)和实现详细信息(如 static)等;并获取或设置字段值。 7.使用 EventInfo 来了解如下的类似信息:事件的名称、事件处理程序数据类型、自定义属性、声明类型和反射类型等;并添加或移除事件处理程序。 8.使用 PropertyInfo 来了解如下的类似信息:属性的名称、数据类型、声明类型、反射类型和只读或可写状态等;并获取或设置属性值。 9.使用 ParameterInfo 来了解如下的类似信息:参数的名称、数据类型、参数是输入参数还是输出参数,以及参数在方法签名中的位置等。

    参考资料:http://ke.baidu.com/view/77128.htm#4

    反射可以让你动态的给程序增加功能。比如,原来窗体上有一个文本框,在里面输入一个jpeg的图片文件名时。程序可以把这个文件转换成bmp格式。如果要增加读png文件并转换的功能,只需要在写一个dll。程序就会根据你在文本框中的输入来决定是否调用这个新的dll来处理你的输入。这个用反射做的话,原来的程序什么都不用动,只需要增加一个dll就可以了。

    想了解反射是做什么用的,首先要了解托管模块、元数据、程序集、应用程序域等概念简单点说,C#的源代码文件,在经过编译器编译后会生成(IL)中间语言和元数据生成的这部份就是托管模块一个或多个托管模块组成程序集,多个程序集又可以组成应用程序域元数据里包含了源代码中所有定义和引用的类型和成员的信息其中,要获取,元数据定义的表就得利用反射机制,例如获取整个程序集的方法名,字段的属性等

    反射叫做reflector以c#为例c#中,所有类型都是以元数据形式保存的可以通过反射取得某个类(或类型)的信息, 调用其私有方法,察看其私有变量一般会在使用NUnit等自动测试工具时用到

    0 讨论(0)
提交回复