C#之反射讲解

C#之反射讲解

C#中的反射(Reflection)是一个强大的功能,它允许程序在运行时(runtime)获取关于类型(Type)的信息(如类的属性、方法、构造函数、字段等),并且可以动态地创建和调用类型。

这意味着程序可以检查自身或其他程序集(assembly)中的类型信息,并在运行时根据需要创建和操作这些类型的实例。

反射的主要用途包括但不限于:

动态加载类型:在运行时加载类型信息,而不需要在编译时知道这些类型。
创建实例:通过反射,可以动态地创建类的实例,而不需要直接调用构造函数。
调用方法:可以获取方法的元数据(如方法名、参数类型等),并动态地调用这些方法。
获取和设置字段值:可以访问和修改私有字段的值(尽管这通常不被推荐,因为它破坏了封装性)。
检查类型信息:可以查询类型是否实现了某个接口、是否继承了某个类、是否包含某个方法等。

反射在以下场景中特别有用:

插件式架构:允许应用程序在运行时加载和扩展功能。
ORM(对象关系映射)框架:如Entity Framework,使用反射来将对象映射到数据库表。
序列化/反序列化:将数据转换为可以存储或传输的格式,并在需要时恢复原始对象。
测试框架:使用反射来自动测试类的所有方法。

然而,反射也有一些缺点:

性能:反射通常比直接调用方法或访问字段要慢得多,因为它涉及到动态类型解析和代码生成。
安全性:反射可以访问私有成员,这可能会破坏封装性并导致安全问题。
复杂性:使用反射的代码通常比直接操作类型要复杂得多。

因此,在使用反射时应该谨慎,并确保你了解它的优缺点以及如何在你的应用程序中适当地使用它。

System.Type 类–通过这个类可以访问任何给定数据类型的信息。
System.Reflection.Assembly 类–它可以用于访问给定程序集的信息,或者把这个程序集加载到程序中。

示例1:获取构造函数及其参数

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ReflectionDemo rc = new ReflectionDemo();

            Console.WriteLine("1. 获取构造函数中的参数");
            // 获取类信息
            Type t= rc.GetType();
            // 获取构造函数
            ConstructorInfo[] infos = t.GetConstructors();
            // 遍历构造函数
            foreach (ConstructorInfo item in infos)
            {
                // 获取构造函数参数
                ParameterInfo[] piArray = item.GetParameters();
                // 输出 参数类型和参数名字
                foreach (ParameterInfo ptem in piArray)
                {
                    Console.WriteLine($"参数类型 {ptem.ParameterType} 参数名字{ptem.Name}");
                }
            }
        }
    }

    public class ReflectionDemo
    {
        public int Id;

        private string _name;
        private int _age;
        private int _sex;

        public string Name { get => _name; set => _name = value; }
        public int Age { get => _age; set => _age = value; }
        public int Sex { get => _sex; set => _sex = value; }

        public ReflectionDemo()
        {

        }
        public ReflectionDemo(int sex)
        {
            this._sex = sex;
        }
        public ReflectionDemo(string name,int age)
        {
            this._name = name;
            this._sex = age;
        }

        public void show()
        {
            Console.WriteLine($"姓名:{_name} 年龄:{_age} 性别:{_sex}");

        }
    }
}

在这里插入图片描述

示例2:通过构造函数动态生成对象

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ReflectionDemo rc = new ReflectionDemo();

            Console.WriteLine("1. 获取构造函数中的参数");
            // 获取类信息
            Type t= rc.GetType();
            // 获取构造函数
            ConstructorInfo[] infos = t.GetConstructors();
            // 遍历构造函数
            foreach (ConstructorInfo item in infos)
            {
                // 获取构造函数参数
                ParameterInfo[] piArray = item.GetParameters();
                // 输出 参数类型和参数名字
                foreach (ParameterInfo ptem in piArray)
                {
                    Console.WriteLine($"参数类型 {ptem.ParameterType} 参数名字{ptem.Name}");
                }
            }

            Console.WriteLine("2. 通过构造函数动态生成对象");
            Type t2 = typeof(ReflectionDemo);
            Type[] pt2 = new Type[2];
            pt2[0] = typeof(string);
            pt2[1] = typeof(int);
            // 通过参数类型,来获取构造函数
            ConstructorInfo ci = t2.GetConstructor(pt2);
            // 构造object对象,同时赋值
            object[] obj = new object[2] { "major", 18 };
            // 调用构造函数生成对象
            object ob = ci.Invoke(obj);
            // 强制类型转换
            ((ReflectionDemo)ob).show();

        }
    }

    public class ReflectionDemo
    {
        public int Id;

        private string _name;
        private int _age;
        private int _sex;

        public string Name { get => _name; set => _name = value; }
        public int Age { get => _age; set => _age = value; }
        public int Sex { get => _sex; set => _sex = value; }

        public ReflectionDemo()
        {

        }
        public ReflectionDemo(int sex)
        {
            this._sex = sex;
        }
        public ReflectionDemo(string name,int age)
        {
            this._name = name;
            this._age = age;
        }

        public void show()
        {
            Console.WriteLine($"姓名:{_name} 年龄:{_age} 性别:{_sex}");

        }
    }
}

在这里插入图片描述

三 通过Activator动态生成对象

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ReflectionDemo rc = new ReflectionDemo();

            Console.WriteLine("1. 获取构造函数中的参数");
            // 获取类信息
            Type t= rc.GetType();
            // 获取构造函数
            ConstructorInfo[] infos = t.GetConstructors();
            // 遍历构造函数
            foreach (ConstructorInfo item in infos)
            {
                // 获取构造函数参数
                ParameterInfo[] piArray = item.GetParameters();
                // 输出 参数类型和参数名字
                foreach (ParameterInfo ptem in piArray)
                {
                    Console.WriteLine($"参数类型 {ptem.ParameterType} 参数名字{ptem.Name}");
                }
            }

            Console.WriteLine("2. 通过构造函数动态生成对象");
            Type t2 = typeof(ReflectionDemo);
            Type[] pt2 = new Type[2];
            pt2[0] = typeof(string);
            pt2[1] = typeof(int);
            // 通过参数类型,来获取构造函数
            ConstructorInfo ci = t2.GetConstructor(pt2);
            // 构造object对象,同时赋值
            object[] obj = new object[2] { "major", 18 };
            // 调用构造函数生成对象
            object ob = ci.Invoke(obj);
            // 强制类型转换
            ((ReflectionDemo)ob).show();


            Console.WriteLine("3. 通过Activator动态生成对象");

            Type t3 = typeof(ReflectionDemo);
            object[] obj3 = new object[2] { "major2", 26 };
            object o3 = Activator.CreateInstance(t3,obj3);
            // 强制类型转换
            ((ReflectionDemo)o3).show();

        }
    }

    public class ReflectionDemo
    {
        public int Id;

        private string _name;
        private int _age;
        private int _sex;

        public string Name { get => _name; set => _name = value; }
        public int Age { get => _age; set => _age = value; }
        public int Sex { get => _sex; set => _sex = value; }

        public ReflectionDemo()
        {

        }
        public ReflectionDemo(int sex)
        {
            this._sex = sex;
        }
        public ReflectionDemo(string name,int age)
        {
            this._name = name;
            this._age = age;
        }

        public void show()
        {
            Console.WriteLine($"姓名:{_name} 年龄:{_age} 性别:{_sex}");

        }
    }
}

在这里插入图片描述

获取属性和字段

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ReflectionDemo rc = new ReflectionDemo();

            Console.WriteLine("1. 获取构造函数中的参数");
            // 获取类信息
            Type t= rc.GetType();
            // 获取构造函数
            ConstructorInfo[] infos = t.GetConstructors();
            // 遍历构造函数
            foreach (ConstructorInfo item in infos)
            {
                // 获取构造函数参数
                ParameterInfo[] piArray = item.GetParameters();
                // 输出 参数类型和参数名字
                foreach (ParameterInfo ptem in piArray)
                {
                    Console.WriteLine($"参数类型 {ptem.ParameterType} 参数名字{ptem.Name}");
                }
            }

            Console.WriteLine("2. 通过构造函数动态生成对象");
            Type t2 = typeof(ReflectionDemo);
            Type[] pt2 = new Type[2];
            pt2[0] = typeof(string);
            pt2[1] = typeof(int);
            // 通过参数类型,来获取构造函数
            ConstructorInfo ci = t2.GetConstructor(pt2);
            // 构造object对象,同时赋值
            object[] obj = new object[2] { "major", 18 };
            // 调用构造函数生成对象
            object ob = ci.Invoke(obj);
            // 强制类型转换
            ((ReflectionDemo)ob).show();


            Console.WriteLine("3. 通过Activator动态生成对象");

            Type t3 = typeof(ReflectionDemo);
            object[] obj3 = new object[2] { "major2", 26 };
            object o3 = Activator.CreateInstance(t3,obj3);
            // 强制类型转换
            ((ReflectionDemo)o3).show();


            Console.WriteLine("4. 查看类中的属性");

            Type t4 = typeof(ReflectionDemo);

            PropertyInfo[] piArray2 = t4.GetProperties();
            foreach (PropertyInfo item in piArray2)
            {
                Console.WriteLine($"类中属性:{item.Name}");
            }


            Console.WriteLine("5. 查看类中的字段");
            Type t5 = typeof(ReflectionDemo);

            FieldInfo[] piArray3 = t5.GetFields();
            foreach (FieldInfo item in piArray3)
            {
                Console.WriteLine($"类中字段:{item.Name}");
            }

        }
    }

    public class ReflectionDemo
    {
        public int Id;

        private string _name;
        private int _age;
        private int _sex;

        public string Name { get => _name; set => _name = value; }
        public int Age { get => _age; set => _age = value; }
        public int Sex { get => _sex; set => _sex = value; }

        public ReflectionDemo()
        {

        }
        public ReflectionDemo(int sex)
        {
            this._sex = sex;
        }
        public ReflectionDemo(string name,int age)
        {
            this._name = name;
            this._age = age;
        }

        public void show()
        {
            Console.WriteLine($"姓名:{_name} 年龄:{_age} 性别:{_sex}");

        }
    }
}

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ReflectionDemo rc = new ReflectionDemo();

            Console.WriteLine("1. 获取构造函数中的参数");
            // 获取类信息
            Type t= rc.GetType();
            // 获取构造函数
            ConstructorInfo[] infos = t.GetConstructors();
            // 遍历构造函数
            foreach (ConstructorInfo item in infos)
            {
                // 获取构造函数参数
                ParameterInfo[] piArray = item.GetParameters();
                // 输出 参数类型和参数名字
                foreach (ParameterInfo ptem in piArray)
                {
                    Console.WriteLine($"参数类型 {ptem.ParameterType} 参数名字{ptem.Name}");
                }
            }

            Console.WriteLine("2. 通过构造函数动态生成对象");
            Type t2 = typeof(ReflectionDemo);
            Type[] pt2 = new Type[2];
            pt2[0] = typeof(string);
            pt2[1] = typeof(int);
            // 通过参数类型,来获取构造函数
            ConstructorInfo ci = t2.GetConstructor(pt2);
            // 构造object对象,同时赋值
            object[] obj = new object[2] { "major", 18 };
            // 调用构造函数生成对象
            object ob = ci.Invoke(obj);
            // 强制类型转换
            ((ReflectionDemo)ob).show();


            Console.WriteLine("3. 通过Activator动态生成对象");

            Type t3 = typeof(ReflectionDemo);
            object[] obj3 = new object[2] { "major2", 26 };
            object o3 = Activator.CreateInstance(t3,obj3);
            // 强制类型转换
            ((ReflectionDemo)o3).show();


            Console.WriteLine("4. 查看类中的属性");

            Type t4 = typeof(ReflectionDemo);

            PropertyInfo[] piArray2 = t4.GetProperties();
            foreach (PropertyInfo item in piArray2)
            {
                Console.WriteLine($"类中属性:{item.Name}");
            }


            Console.WriteLine("5. 查看类中的字段");
            Type t5 = typeof(ReflectionDemo);

            FieldInfo[] piArray3 = t5.GetFields();
            foreach (FieldInfo item in piArray3)
            {
                Console.WriteLine($"类中字段:{item.Name}");
            }

            Console.WriteLine("5. 查看类中的方法");
            Type t6 = typeof(ReflectionDemo);

            MethodInfo[] piArray4 = t6.GetMethods();
            foreach (MethodInfo item in piArray4)
            {
                // 获取方法的参数
                Console.WriteLine($"类中方法:{item.Name}");
            }

        }
    }

    public class ReflectionDemo
    {
        public int Id;

        private string _name;
        private int _age;
        private int _sex;

        public string Name { get => _name; set => _name = value; }
        public int Age { get => _age; set => _age = value; }
        public int Sex { get => _sex; set => _sex = value; }

        public ReflectionDemo()
        {

        }
        public ReflectionDemo(int sex)
        {
            this._sex = sex;
        }
        public ReflectionDemo(string name,int age)
        {
            this._name = name;
            this._age = age;
        }

        public void show()
        {
            Console.WriteLine($"姓名:{_name} 年龄:{_age} 性别:{_sex}");

        }



    }
}

在这里插入图片描述

综合

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ReflectionDemo rc = new ReflectionDemo();

            Console.WriteLine("1. 获取构造函数中的参数");
            // 获取类信息
            Type t= rc.GetType();
            // 获取构造函数
            ConstructorInfo[] infos = t.GetConstructors();
            // 遍历构造函数
            foreach (ConstructorInfo item in infos)
            {
                // 获取构造函数参数
                ParameterInfo[] piArray = item.GetParameters();
                // 输出 参数类型和参数名字
                foreach (ParameterInfo ptem in piArray)
                {
                    Console.WriteLine($"参数类型 {ptem.ParameterType} 参数名字{ptem.Name}");
                }
            }

            Console.WriteLine("2. 通过构造函数动态生成对象");
            Type t2 = typeof(ReflectionDemo);
            Type[] pt2 = new Type[2];
            pt2[0] = typeof(string);
            pt2[1] = typeof(int);
            // 通过参数类型,来获取构造函数
            ConstructorInfo ci = t2.GetConstructor(pt2);
            // 构造object对象,同时赋值
            object[] obj = new object[2] { "major", 18 };
            // 调用构造函数生成对象
            object ob = ci.Invoke(obj);
            // 强制类型转换
            ((ReflectionDemo)ob).show();


            Console.WriteLine("3. 通过Activator动态生成对象");

            Type t3 = typeof(ReflectionDemo);
            object[] obj3 = new object[2] { "major2", 26 };
            object o3 = Activator.CreateInstance(t3,obj3);
            // 强制类型转换
            ((ReflectionDemo)o3).show();


            Console.WriteLine("4. 查看类中的属性");

            Type t4 = typeof(ReflectionDemo);

            PropertyInfo[] piArray2 = t4.GetProperties();
            foreach (PropertyInfo item in piArray2)
            {
                Console.WriteLine($"类中属性:{item.Name}");
            }


            Console.WriteLine("5. 查看类中的字段");
            Type t5 = typeof(ReflectionDemo);

            FieldInfo[] piArray3 = t5.GetFields();
            foreach (FieldInfo item in piArray3)
            {
                Console.WriteLine($"类中字段:{item.Name}");
            }

            Console.WriteLine("6. 查看类中的方法");
            Type t6 = typeof(ReflectionDemo);

            MethodInfo[] piArray4 = t6.GetMethods();
            foreach (MethodInfo item in piArray4)
            {
                // 获取方法的参数
                Console.WriteLine($"类中方法:{item.Name}");
            }

            Console.WriteLine("7. 综合");

            Type t7 = typeof(ReflectionDemo);
            object o7 = Activator.CreateInstance(t7);
            FieldInfo fi7 = t7.GetField("Id");
            fi7.SetValue(o7, 123);

            PropertyInfo pname = t7.GetProperty("Name");
            pname.SetValue(o7, "major7");
            PropertyInfo page = t7.GetProperty("Age");
            page.SetValue(o7, 1);
            PropertyInfo psex = t7.GetProperty("Sex");
            psex.SetValue(o7, 26);


            MethodInfo pshow = t7.GetMethod("show");
            pshow.Invoke(o7, null);

            Console.WriteLine($"ID:{((ReflectionDemo)o7).Id}");

        }
    }

    public class ReflectionDemo
    {
        public int Id;

        private string _name;
        private int _age;
        private int _sex;

        public string Name { get => _name; set => _name = value; }
        public int Age { get => _age; set => _age = value; }
        public int Sex { get => _sex; set => _sex = value; }

        public ReflectionDemo()
        {

        }
        public ReflectionDemo(int sex)
        {
            this._sex = sex;
        }
        public ReflectionDemo(string name,int age)
        {
            this._name = name;
            this._age = age;
        }

        public void show()
        {
            Console.WriteLine($"姓名:{_name} 年龄:{_age} 性别:{_sex}");

        }



    }
}

在这里插入图片描述

反射程序集

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("1.加载程序集");
            // 加载程序集
            Assembly assembly = Assembly.Load("RFDll");
            // 获取程序集中所有对象
            Type[] types = assembly.GetTypes();
            // 遍历查看
            foreach (Type t in types)
            {

                Console.WriteLine($"{t.FullName}");
            }
            Console.WriteLine("2.加载程序集");
            // 加载程序集
            Assembly assembly2 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types2 = assembly2.GetTypes();
            // 遍历查看
            foreach (Type t in types2)
            {

                Console.WriteLine($"{t.FullName}");
            }

            Console.WriteLine("3.加载程序集单个对象");
            // 加载程序集
            Assembly assembly3 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type type= assembly.GetType("RFDll.Model.Student");
            Console.WriteLine(type.FullName);

        }
    }
}

在这里插入图片描述

加载程序集单个对象的方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("1.加载程序集");
            // 加载程序集
            Assembly assembly = Assembly.Load("RFDll");
            // 获取程序集中所有对象
            Type[] types = assembly.GetTypes();
            // 遍历查看
            foreach (Type t in types)
            {

                Console.WriteLine($"{t.FullName}");
            }
            Console.WriteLine("2.加载程序集");
            // 加载程序集
            Assembly assembly2 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types2 = assembly2.GetTypes();
            // 遍历查看
            foreach (Type t in types2)
            {

                Console.WriteLine($"{t.FullName}");
            }

            Console.WriteLine("3.加载程序集单个对象");
            // 加载程序集
            Assembly assembly3 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type type= assembly.GetType("RFDll.Model.Student");
            Console.WriteLine(type.FullName);


            Console.WriteLine("4.加载程序集单个对象的方法");
            // 加载程序集
            Assembly assembly4 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type type4 = assembly.GetType("RFDll.Model.Student");
            object o = Activator.CreateInstance(type);
            MethodInfo mi = type4.GetMethod("show");
            mi.Invoke(o, null);
        }
    }
}

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("1.加载程序集");
            // 加载程序集
            Assembly assembly = Assembly.Load("RFDll");
            // 获取程序集中所有对象
            Type[] types = assembly.GetTypes();
            // 遍历查看
            foreach (Type t in types)
            {

                Console.WriteLine($"{t.FullName}");
            }
            Console.WriteLine("2.加载程序集");
            // 加载程序集
            Assembly assembly2 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types2 = assembly2.GetTypes();
            // 遍历查看
            foreach (Type t in types2)
            {

                Console.WriteLine($"{t.FullName}");
            }

            Console.WriteLine("3.加载程序集单个对象");
            // 加载程序集
            Assembly assembly3 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type type= assembly.GetType("RFDll.Model.Student");
            Console.WriteLine(type.FullName);


            Console.WriteLine("4.加载程序集单个对象的方法");
            // 加载程序集
            Assembly assembly4 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type type4 = assembly.GetType("RFDll.Model.Student");
            object o = Activator.CreateInstance(type);
            MethodInfo mi = type4.GetMethod("show");
            mi.Invoke(o, null);


            Console.WriteLine("5.加载程序集所有对象的show方法");
            // 加载程序集
            Assembly assembly5 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types5 = assembly.GetTypes();
            foreach (Type ty in types5)
            {
                if(ty.IsClass)
                {
                    object o5 = Activator.CreateInstance(ty);
                    MethodInfo mi2 = ty.GetMethod("show");
                    if (mi2 != null)
                      mi2.Invoke(o5, null);
                }
            }
        }
    }
}

在这里插入图片描述

获取程序集中所有类的字段

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("1.加载程序集");
            // 加载程序集
            Assembly assembly = Assembly.Load("RFDll");
            // 获取程序集中所有对象
            Type[] types = assembly.GetTypes();
            // 遍历查看
            foreach (Type t in types)
            {

                Console.WriteLine($"{t.FullName}");
            }
            Console.WriteLine("2.加载程序集");
            // 加载程序集
            Assembly assembly2 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types2 = assembly2.GetTypes();
            // 遍历查看
            foreach (Type t in types2)
            {

                Console.WriteLine($"{t.FullName}");
            }

            Console.WriteLine("3.加载程序集单个对象");
            // 加载程序集
            Assembly assembly3 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type type= assembly.GetType("RFDll.Model.Student");
            Console.WriteLine(type.FullName);


            Console.WriteLine("4.加载程序集单个对象的方法");
            // 加载程序集
            Assembly assembly4 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type type4 = assembly.GetType("RFDll.Model.Student");
            object o = Activator.CreateInstance(type);
            MethodInfo mi = type4.GetMethod("show");
            mi.Invoke(o, null);


            Console.WriteLine("5.加载程序集所有对象的show方法");
            // 加载程序集
            Assembly assembly5 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types5 = assembly5.GetTypes();
            foreach (Type ty in types5)
            {
                if(ty.IsClass)
                {
                    object o5 = Activator.CreateInstance(ty);
                    MethodInfo mi2 = ty.GetMethod("show");
                    if (mi2 != null)
                      mi2.Invoke(o5, null);
                }
            }


            Console.WriteLine("6.加载程序集所有字段");
            // 加载程序集
            Assembly assembly6 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types6 = assembly6.GetTypes();
            foreach (Type ty in types6)
            {
                if (ty.IsClass)
                {
                    object o6 = Activator.CreateInstance(ty);


                    FieldInfo[] fields = ty.GetFields();

                    foreach (FieldInfo fi in fields)
                    {
                        Console.WriteLine($"字段类型:{fi.FieldType}+字段名称:{fi.Name}");
                    }

                }
            }




        }
    }
}

在这里插入图片描述

获取私有字段

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("1.加载程序集");
            // 加载程序集
            Assembly assembly = Assembly.Load("RFDll");
            // 获取程序集中所有对象
            Type[] types = assembly.GetTypes();
            // 遍历查看
            foreach (Type t in types)
            {

                Console.WriteLine($"{t.FullName}");
            }
            Console.WriteLine("2.加载程序集");
            // 加载程序集
            Assembly assembly2 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types2 = assembly2.GetTypes();
            // 遍历查看
            foreach (Type t in types2)
            {

                Console.WriteLine($"{t.FullName}");
            }

            Console.WriteLine("3.加载程序集单个对象");
            // 加载程序集
            Assembly assembly3 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type type= assembly.GetType("RFDll.Model.Student");
            Console.WriteLine(type.FullName);


            Console.WriteLine("4.加载程序集单个对象的方法");
            // 加载程序集
            Assembly assembly4 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type type4 = assembly.GetType("RFDll.Model.Student");
            object o = Activator.CreateInstance(type);
            MethodInfo mi = type4.GetMethod("show");
            mi.Invoke(o, null);


            Console.WriteLine("5.加载程序集所有对象的show方法");
            // 加载程序集
            Assembly assembly5 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types5 = assembly5.GetTypes();
            foreach (Type ty in types5)
            {
                if(ty.IsClass)
                {
                    object o5 = Activator.CreateInstance(ty);
                    MethodInfo mi2 = ty.GetMethod("show");
                    if (mi2 != null)
                      mi2.Invoke(o5, null);
                }
            }


            Console.WriteLine("6.加载程序集所有字段");
            // 加载程序集
            Assembly assembly6 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types6 = assembly6.GetTypes();
            foreach (Type ty in types6)
            {
                if (ty.IsClass)
                {
                    object o6 = Activator.CreateInstance(ty);


                    FieldInfo[] fields = ty.GetFields();

                    foreach (FieldInfo fi in fields)
                    {
                        Console.WriteLine($"字段类型:{fi.FieldType}+字段名称:{fi.Name}");
                    }

                }
            }

            Console.WriteLine("6.加载程序集所有私有字段");
            // 加载程序集
            Assembly assembly7 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types7 = assembly7.GetTypes();
            foreach (Type ty in types7)
            {
                if (ty.IsClass)
                {
                    object o6 = Activator.CreateInstance(ty);


                    FieldInfo[] fields = ty.GetFields(BindingFlags.NonPublic|BindingFlags.Instance);

                    foreach (FieldInfo fi in fields)
                    {
                        Console.WriteLine($"字段类型:{fi.FieldType}+字段名称:{fi.Name}");
                    }

                }
            }



        }
    }
}

在这里插入图片描述

8.加载程序集所有属性

            Console.WriteLine("8.加载程序集所有属性");
            // 加载程序集
            Assembly assembly8 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types8 = assembly8.GetTypes();
            foreach (Type ty in types8)
            {
                if (ty.IsClass)
                {

                    PropertyInfo[] propertyInfos = ty.GetProperties();

                    foreach (PropertyInfo pp in propertyInfos)
                    {
                        Console.WriteLine($"字段类型:{pp.PropertyType}+字段名称:{pp.Name}");
                    }

                }
            }

在这里插入图片描述

加载程序集所有静态方法

            Console.WriteLine("9.加载程序集所有静态方法");
            // 加载程序集
            Assembly assembly9 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types9 = assembly9.GetTypes();
            foreach (Type ty in types9)
            {
                if (ty.IsClass)
                {
                    object o9 = Activator.CreateInstance(ty);
                    MethodInfo mi9 = ty.GetMethod("show",BindingFlags.Public|BindingFlags.Static);
                    if (mi9 != null)
                        mi9.Invoke(o9, null);

                }
            }

在这里插入图片描述

            Console.WriteLine("10.加载程序集接口");
            // 加载程序集
            Assembly assembly10 = Assembly.LoadFrom("RFDll.dll");
            // 获取程序集中所有对象
            Type[] types10 = assembly10.GetTypes();
            foreach (Type ty in types10)
            {
                if (ty.GetInterface("IStudent")!=null)
                {
                    IStudent i10 = (IStudent)Activator.CreateInstance(ty);
                    Console.WriteLine($"接口 {i10}");
                }
            }

在这里插入图片描述

  • 18
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值