zoukankan      html  css  js  c++  java
  • C# 反射

      写在前面:新手试车,误撞轻拍!

    一.反射定义

      通过反射了解元数据,程序集等模块的内部结构、属性、定义、函数、以及各个模块的基本信息。

      应用:当出现个陌生dll文件或者程序集模块等等类似结构体的时候,可以通过反射查明其内部各函数,方法,属性,成员,事件等等详细信息,然后正确对其进行调用。

    二.反射核心类型

       Assembly                该类型包含了很多方法,通过它可以加载、了解和操纵一个程序集
         Assemblyname        使用该类可以找到大量隐藏在程序集的身份中的细节(版本信息、区域信息)
         Eventinfo                该类保存给定事件的信息
         Fieldinfo                 该类保存给定字段的信息
         Memberinfo            该类包含给定发放的信息
         Methodinfo             该类包含给定方法的信息
         Module                   该类是你可以访问带有多文件程序集的给定模块
         Parameterinfo         该类保存给定参数的信息
         Propertyinfo            该类保存给定属性的信息

    三.Type类型

      1.System.Object.GetType()

      例:

     public void GetType()
            {
                ArrayList ar = new ArrayList();
                Type type = ar.GetType();
             }

      注意:使用该方法必须得到类型的编译时信息,并且在当前内存中有类型实例。

      

      2.System.Type.GetType()

      例:

    public void GetType2()
            {
                Type type = Type.GetType("ArrayList", false, true);
            }

      使用该方法不需要得到类型编译时信息

      GetType()的三个参数:参数,是否抛出异常,是否区分大小写。

      3.typeof

     public void GetType3()
            {
                Type type = typeof(int);
                MessageBox.Show(type.Name);
            }

      使用该方法必须得到类型的编译时信息。

    四.反射

      1.反射方法信息

      static void methods(Type t)
            {
                MethodInfo[] mi = t.GetMethods();
                foreach (var item in mi)
                {
                    MessageBox.Show(item.ToString());
                }
            }

      

      2.反射字段

     static void Fields(Type t)
            {
                FieldInfo[] fi = t.GetFields();
                foreach (var item in fi)
                {
                    MessageBox.Show(item.ToString());
                }
            }

      3.反射接口

     static void interfaces(Type t)
            {
                Type[] inter = t.GetInterfaces();
                foreach (var item in inter)
                {
                    MessageBox.Show(item.ToString());
                }
            }

      4.反射属性

      static void proptry(Type t)
            {
                PropertyInfo[] inter = t.GetProperties();
                foreach (var item in inter)
                {
                    MessageBox.Show(item.ToString());
                }
            }

      5.反射其他基本信息

      static void data(Type t)
            {
                MessageBox.Show(t.BaseType.ToString(), t.IsAbstract.ToString());
            }

      Type的基本信息有很多,读者可根据MSDN上Type的属性信息查看和调用。

      6.实现

    public MainWindow()
            {
                InitializeComponent();
                Type type = Type.GetType("System.Int32");//大小写完全一致
                methods(type);
                Fields(type);
                interfaces(type);
                data(type);
                Ttext.Text = retdata;
               
            }

    四.实例

  • 相关阅读:
    内存映射的原理
    Intel 面试(就不该报外企,英语是硬伤)
    基于多进程和基于多线程服务器的优缺点及nginx服务器的启动过程
    pdflush机制
    百度面试
    同步IO和异步IO的区别
    阿里面试
    linux内核学习之四:进程切换简述
    static成员函数不能调用non-static成员函数
    C/C++函数调用方式
  • 原文地址:https://www.cnblogs.com/Khan-Sadas/p/5253439.html
Copyright © 2011-2022 走看看