详解C#特性和反射(二)

使用反射(Reflection)使得程序在运行过程中可以动态的获取对象或类型的类型信息,然后调用该类型的方法和构造函数,或访问和修改该类型的字段和属性;可以通过晚期绑定技术动态的创建类型的实例;可以获取程序集中的所有类型信息;可以在动态构建新类型;还可以检索元素所添加的特性;   ※反射相关的类基本都位于命名空间System.Reflection中;   ※动态构建新类型的类位于命名空间System.Reflection.Emit中;   一、访问或修改类型的实例、静态字段: 复制代码 public class MyClass { public int myField; public static int myStaticField; } //使用方式: //访问或修改类型的实例字段myField MyClass myObj = new MyClass() { myField = 1 }; //创建实例 Type myType = typeof(MyClass); //获取类型,或myObj.GetType() FieldInfo fieldInfo = myType.GetField("myField"); //获取类型中指定的字段信息 Console.WriteLine((int)fieldInfo.GetValue(myObj)); //1,获取实例字段的值 fieldInfo.SetValue(myObj, 2); //给实例字段赋值 //访问或修改类型的静态字段myStaticField FieldInfo staticFieldInfo = myType.GetField("myStaticField"); //获取类型中指定的字段信息 Console.WriteLine(staticFieldInfo.GetValue(null)); //0,获取静态字段的值 staticFieldInfo.SetValue(null, 2); //给静态字段赋值 复制代码   ※与直接赋值相比,使用反射赋值用时约长75倍,使用以下代码多次测试: 复制代码 public class MyClass { public int myField; } class Program { static void Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); MyClass myObj = new MyClass() { myField = 1 }; Type myType = typeof(MyClass); FieldInfo fieldInfo = myType.GetField("myField"); stopwatch.Start(); for (int i = 0; i < 10_000_000; i++) { fieldInfo.SetValue(myObj, 2); } stopwatch.Stop(); Console.WriteLine($"使用反射赋值1千万次耗时:{stopwatch.ElapsedMilliseconds}"); stopwatch.Reset(); stopwatch.Start(); for (int i = 0; i < 10_000_000; i++) { myObj.myField = 2; } stopwatch.Stop(); Console.WriteLine($"直接赋值1千万次耗时:{stopwatch.ElapsedMilliseconds}"); Console.Read(); } } 复制代码   二、访问或修改类型的实例、静态属性: 复制代码 public class MyClass { public int MyProperty { get; set; } public static int MyStaticProperty { get; set; } } //使用方式: //访问或修改类型的实例属性MyProperty MyClass myObj = new MyClass() { MyProperty = 1 }; //创建实例 Type myType = typeof(MyClass); //获取类型,或myObj.GetType() PropertyInfo propertyInfo = myType.GetProperty("MyProperty"); //获取类型中指定的属性信息 Console.WriteLine((int)propertyInfo.GetValue(myObj, null)); //1,获取实例属性的值 propertyInfo.SetValue(myObj, 2, null); //给实例属性赋值 //访问或修改类型的静态属性MyStaticProperty PropertyInfo staticPropertyInfo = myType.GetProperty("MyStaticProperty"); //获取类型中指定的属性信息 Console.WriteLine(staticPropertyInfo.GetValue(null, null)); //0,获取静态属性的值 staticPropertyInfo.SetValue(null, 2); //给静态属性赋值 复制代码   ※在使用反射给属性赋值时,如果该属性不具有set访问器,则会抛出异常ArgumentException;   三、调用类型的方法: 复制代码 public class MyClass {   public void MyFunc(int num)   {     Console.WriteLine("MyFunc(int num) execute, the parameter is: " + num);   }   public static void MyStaticFunc(int num)   {   Console.WriteLine("MyStaticFunc(int num) execute, the parameter is: " + num);   } } //使用方式: //调用类型的实例方法MyFunc MyClass myObj = new MyClass(); //创建实例 Type myType = typeof(MyClass); //获取类型,或myObj.GetType() MethodInfo methodInfo = myType.GetMethod("MyFunc"); //获取类型中指定的方法信息 methodInfo.Invoke(myObj, new object[] { 10 }); //调用实例方法,并传入参数,无参传null //MyFunc(int num) execute, the parameter is: 10 //调用类型的实例方法MyStaticFunc MethodInfo staticMethodInfo = myType.GetMethod("MyStaticFunc"); //获取类型中指定的方法信息 staticMethodInfo.Invoke(null, new object[] { 20 }); //调用静态方法,并传入参数,无参传null //MyStaticFunc(int num) execute, the parameter is: 20 复制代码   四、调用类型的构造函数同时创建实例: 复制代码 public class MyClass { public MyClass() { Console.WriteLine("MyClass() execute."); } public MyClass(int num) { Console.WriteLine("MyClass(int num) execute, the parameter is: " + num); } } //使用方式: //调用无参的构造函数 Type myType = typeof(MyClass); //获取类型,或myObj.GetType() ConstructorInfo constructorInfo = myType.GetConstructor(new Type[] { }); //获取类型中指定的构造函数信息,传入该构造函数的参数列表的类型数组,无参传空数组 MyClass myObj = constructorInfo.Invoke(null) as MyClass; //通过调用构造函数创建实例,无参传null //MyClass() execute. //调用带参数的构造函数 constructorInfo = myType.GetConstructor(new Type[] { typeof(int) }); //获取类型中指定的构造函数信息,传入该构造函数的参数列表的类型数组 myObj = constructorInfo.Invoke(new object[] { 20 }) as MyClass; //通过调用构造函数创建实例,并传入参数 //MyClass(int num) execute, the parameter is: 20 复制代码   ※也可以使用Type类中的实例方法InvokeMember()来调用指定成员;   五、使用反射查找特性:   1.如果元素使用了特性,在没有检索并对其进行操作前该特性没有任何价值,可以使用反射在程序运行过程中获取元素添加的特性然后对其进行操作,使用特性基类Attribute中的静态方法GetCustomAttributes(MemberInfo element)或命名空间System.Reflection中的扩展方法GetCustomAttributes(this MemberInfo element)来获取类型或成员的所有特性信息: 复制代码 Attribute[] attributes = Attribute.GetCustomAttributes(typeof(MyClass)); //IEnumerable attributes = typeof(MyClass).GetCustomAttributes(); foreach (var item in attributes) {   if (item is MyselfAttribute)   {     MyselfAttribute attribute = item as MyselfAttribute;     Console.WriteLine(attribute .ClassName + " " + attribute .Author); //MyClass Me   } } 复制代码   2.这两个方法都有对应的重载方法,可以传入要检索的指定特性的类型,这样即可得到元素中所有指定类型的特性信息: 复制代码 Attribute[] attributes = Attribute.GetCustomAttributes(typeof(MyClass), typeof(MyselfAttribute)); //IEnumerable attributes = typeof(MyClass).GetCustomAttributes(typeof(MyselfAttribute)); foreach (var item in attributes) {   MyselfAttribute attribute = item as MyselfAttribute;   Console.WriteLine(attribute.ClassName + " " + attribute.Author); //MyClass Me } 复制代码   ※如果未找到任何特性或指定类型的特性,这些方法会返回一个空数组;   3.也可以使用基类Attribute中的静态方法GetCustomAttribute(MemberInfo element, Type attributeType)或命名空间System.Reflection中的扩展方法GetCustomAttribute(this MemberInfo element, Type attributeType)来获取类型或成员的指定特性信息;   ※如果未找到指定类型的特性,会返回null;   ※在检索的元素中存在多个相同的指定类型的特性时,会抛出异常Reflection.AmbiguousMatchException;   类型信息、晚期绑定、动态创建类型等会在下一篇中介绍。 如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的认可是我写作的最大动力! 作者:Minotauros 出处:https://www.cnblogs.com/minotauros/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。https://www.cnblogs.com/minotauros/p/9709491.html
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信