1.反射可以做什么?它可以动态创建对象,动态赋值,动态调用方法。
    2..NET中的类都被编译成IL,反射可以再运行时获得类的信息。例如:获取哪些方法、字段、构造函数,父类等等,还可以动态创建对 象、调用成员。
    3.每个类对应着一个Type对象,而每个方法对应着MethodInfo对象,每个属性对应一个PropertyInfo.这些就是类、方法、属性的“元数据”。这些“元数据对象”和成员有关,和对象无关,每个成员对应一个对象。
    创建一个学生类:

    1. public class Student
    2. {
    3. public Student(string name, int age,string sex)
    4. {
    5. this.name = name;
    6. this.age = age;
    7. this.sex = sex;
    8. }
    9. public Student(string sex)
    10. {
    11. this.sex = sex;
    12. }
    13. public Student()
    14. { }
    15. public int id;
    16. private string name;
    17. /// <summary>
    18. /// 姓名
    19. /// </summary>
    20. public string Name
    21. {
    22. get { return name; }
    23. set { name = value; }
    24. }
    25. private int age;
    26. /// <summary>
    27. /// 年龄
    28. /// </summary>
    29. public int Age
    30. {
    31. get { return age; }
    32. set { age = value; }
    33. }
    34. private string sex;
    35. /// <summary>
    36. /// 性别
    37. /// </summary>
    38. public string Sex
    39. {
    40. get { return sex; }
    41. set { sex = value; }
    42. }
    43. public void Show()
    44. {
    45. Console.WriteLine("姓名:" + name + "\n" + "年龄:" + age + "\n" + "性别:" + sex);
    46. }
    47. }

    获取该类的几种写法:

    1. static void Main(string[] args) {
    2. Student student = new Student();
    3.  Type type1 = student.GetType();
    4. Type type2 = typeof(Student);
    5. Type type3 = Type.GetType("ReflexDemo.Models.Student");
    6. Console.WriteLine(object.ReferenceEquals(type1, type2)); Console.WriteLine(object.ReferenceEquals(type2, type3));
    7. Console.ReadLine();
    8. }

    结果:
    C#反射 - 图1
    说明type1、type2、type3是同一个对象。
    类的实例成员:在vs编译器中可以选择Type按F12进去看api文档

    1. Type type = typeof(Student);
    2. //BaseType该类型的父类
    3. Console.WriteLine(type.GetType().BaseType.BaseType);

    结果:
    C#反射 - 图2
    说明该类型父类的父类就是Type。
    FieldInfo、PropertyInfo、MethodInfo、ConstructorInfo的父类和各种成员:
    按F12我们可以看到FieldInfo、PropertyInfo都是继承MemberInfo。MethodInfo、ConstructorInfo继承MethodBase,而MemberInfo有是继承MethodBase。从而推出FieldInfo、PropertyInfo父类的父类就是MethodBase。
    C#反射 - 图3
    C#反射 - 图4
    C#反射 - 图5
    C#反射 - 图6
    C#反射 - 图7详细的类使用成员可以去查文档。
    利用反射查看类中的属性:

    1. /// <summary>
    2. /// 查看类中属性
    3. /// </summary>
    4. public static void Properties()
    5. {
    6. Student Student = new Student(); //当前类中所有的公共属性
    7. PropertyInfo[] infos = Student.GetType().GetProperties();
    8. foreach (PropertyInfo pi in infos)
    9. {
    10. Console.WriteLine(pi.Name);
    11. }
    12. }

    结果:
    C#反射 - 图8
    利用反射查看类中字段:

    1. /// <summary>
    2. /// 类中字段
    3. /// </summary>
    4. public static void Field()
    5. {
    6. Student Student = new Student(); //当前所有的公共字段
    7. FieldInfo[] info = Student.GetType().GetFields();
    8. foreach (FieldInfo fi in info)
    9. {
    10. Console.WriteLine(fi.Name);
    11. }
    12. }

    结果:
    C#反射 - 图9
    利用反射获取类中的方法:

    1. /// <summary>
    2. /// 类中方法
    3. /// </summary>
    4. public static void Method()
    5. {
    6. Student Student = new Student(); //当前类中所有的公共方法
    7. MethodInfo[] infos = Student.GetType().GetMethods();
    8. foreach (MethodInfo mi in infos)
    9. {
    10. Console.WriteLine(mi.ReturnType + "\n" + mi.Name + "\n");
    11. }
    12. #region 获取单个方法
    13. MethodInfo info = Student.GetType().GetMethod("Show");
    14. Console.WriteLine(info.ReturnType + "\n" + info.Name + "\n");
    15. #endregion
    16. }

    结果:
    C#反射 - 图10
    利用反射获取构造函数:

    1. /// <summary>
    2. /// 反射构造函数
    3. /// </summary>
    4. public static void Constructor()
    5. {
    6. Student Student = new Student();
    7. Type type = Student.GetType();
    8. //获取所有的公共构造函数
    9. ConstructorInfo[] infos = type.GetConstructors();
    10. foreach (ConstructorInfo ci in infos)
    11. {
    12. ParameterInfo[] parameterInfos = ci.GetParameters();
    13. foreach (ParameterInfo pi in parameterInfos)
    14. {
    15. //参数类型和参数名称
    16. Console.WriteLine(pi.ParameterType.ToString() + "\n" + pi.Name + "\n");
    17. }
    18. }
    19. Console.ReadLine();
    20. }

    结果:
    C#反射 - 图11
    利用反射用构造函数生成对象:

    1. /// <summary>
    2. /// 用构造函数生成对象
    3. /// </summary>
    4. public static void ObjectPro()
    5. {
    6. Type type = typeof(Student);
    7. Type[] types = new Type[3];
    8. types[0] = typeof(string);
    9. types[1] = typeof(int);
    10. types[2] = typeof(string);
    11. ConstructorInfo ci = type.GetConstructor(types);
    12. object[] obj = new object[3] { "Admin", 21, "男" };
    13. #region 调用构造函数生成对象
    14. object o = ci.Invoke(obj);
    15. #endregion
    16. #region 用Activator的CreateInstance静态方法,生成新对象
    17. //object o = Activator.CreateInstance(type, obj);
    18. #endregion
    19. ((Student)o).Show();
    20. }

    结果:
    C#反射 - 图12
    反射类:

    1. /// <summary>
    2. /// 反射类
    3. /// </summary>
    4. public static void Assemblys()
    5. {
    6. Assembly assembly = Assembly.Load("ReflexDemo");
    7. Type t = assembly.GetType("ReflexDemo.Models.Student"); //参数必须是类的全名
    8. object o = Activator.CreateInstance(t, "男");
    9. MethodInfo mi = t.GetMethod("Show");
    10. //调用当前实例
    11. mi.Invoke(o, null);
    12. }

    结果:
    C#反射 - 图13
    反射DLL:首先你的dll文件要放到该项目的bin/Debug下面:
    C#反射 - 图14

    1. /// <summary>
    2. /// 反射DLL
    3. /// </summary>
    4. public static void AssmblysDll() {
    5. Assembly assembly = Assembly.LoadFrom("Newtonsoft.Json.dll");
    6. Type[] tArray = assembly.GetTypes();
    7. foreach (Type t in tArray)
    8. {
    9. Console.WriteLine(t.Name);
    10. }
    11. }

    结果:
    C#反射 - 图15
    反射动态创建类:

    1. /// <summary>
    2. /// 动态创建类
    3. /// </summary>
    4. public static void OperationrRef()
    5. {
    6. Student Student = new Student();
    7. Type t = Student.GetType();
    8. //动态创建类,这个类必须要public且无参构造函数
    9. object o = Activator.CreateInstance(t);
    10. FieldInfo info = t.GetField("id");
    11. //给ID字段赋值
    12. info.SetValue(o, 4);
    13. PropertyInfo propertyInfoName = t.GetProperty("Name");
    14. //属性赋值
    15. propertyInfoName.SetValue(o, "Admin", null);
    16. PropertyInfo propertyInfoAge = t.GetProperty("Age");
    17. propertyInfoAge.SetValue(o, 18, null);
    18. PropertyInfo propertyInfoSex = t.GetProperty("Sex");
    19. propertyInfoSex.SetValue(o, "男", null);
    20. MethodInfo memberInfo = t.GetMethod("Show");
    21. memberInfo.Invoke(o, null);
    22. Console.WriteLine("ID为:" + ((Student)o).id);
    23. }

    结果:
    C#反射 - 图16