为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
C#

  使用反射(Reflection)使得程序在运行过程中可以动态的获取对象或类型的类型信息,然后调用该类型的方法和构造函数,或访问和修改该类型的字段和属性;可以通过晚期绑定技术动态的创建类型的实例;可以获取程序集中的所有类型信息;可以在动态构建新类型;还可以检索元素所添加的特性;
  ※反射相关的类基本都位于命名空间System.Reflection中;
  ※动态构建新类型的类位于命名空间System.Reflection.Emit中;


  一、访问或修改类型的实例、静态字段:


public class MyClass
{    public int myField;    public static int myStaticField;
}//使用方式://访问或修改类型的实例字段myFieldMyClass 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); //给实例字段赋值//访问或修改类型的静态字段myStaticFieldFieldInfo 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; }
}
//使用方式://访问或修改类型的实例属性MyPropertyMyClass 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); //给实例属性赋值//访问或修改类型的静态属性MyStaticPropertyPropertyInfo 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);
  }
}
//使用方式://调用类型的实例方法MyFuncMyClass 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//调用类型的实例方法MyStaticFuncMethodInfo 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<Attribute> 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<Attribute> 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/p/9709491.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
33
获赞与收藏
206

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消