当前位置: 代码迷 >> 综合 >> C#学习(高级课程)Day13——反射
  详细解决方案

C#学习(高级课程)Day13——反射

热度:80   发布时间:2023-12-05 15:42:19.0

4.6  反射  将类给拆解
1.反射(私有的,字符串、或者特性才使用)
2.命名空间
3.Type
4.访问私有的 BingdingFlags
5.成员:
MemberInfo
6.字段:(区分静态,非静态的)
FieldInfo
实例的公共字段、实例的私有字段
静态的公共字段、静态的私有字段
getValue  ,setvalue
7.了解Type的属性:(区分静态,非静态的)
ProPertyInfo
GetProperty(), GetProperties()
公共的实例属性、公共的静态属性
私有的实例属性、私有的静态属性      
getValue,setvalue 获取和赋值
8.了解Type的方法
personType.getMethod()  、personType.getMethods()    
MethodInfo
默认是公共方法
公共的实例方法、公共的静态方法
私有的实例方法、私有的静态方法
继承自Object的四个方法 :equals、gethashcode、gettype、tostring
成员方法
(1)无参数、(无返回值)、公共的
获取:Methodinfo method  =
调用:
(2)有参数、(无返回值)、公共的(面对重载如何传参)
获取:
调用:
(3)有参数、私有的
获取:personType
调用:
静态方法
(1)公共的:
(2)私有的:
9.反射方式实例化一个对象
调用共有的
(1)实例化某个类型的对象,调用公共的无参构造函数\返回一个Object
Activator.CreateInstance()  //
调用私有的
(2)实例化某个类型的对象,调用私有的无参构造函数
Activator.CreateInstance(  , nonPublic)
(3)实例化某个类型的对象,调用私有的有参构造函数
(4)实例化某个类型的对象,调用公有的有参构造函数

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using MyNamespace;
using UnityEngine;public class LearnReflectionTest : MonoBehaviour
{private void Start(){#region 获取类型//第一种:通过类获取类的类型//通过typeof获取Person类的类型//注意:typeof中的类型一定是可以访问到的Type personType = typeof(Person);Debug.Log(personType);//第二种:通过对象获取类的类型//通过GetType()获取对象所属类的类型Person xiaoming = new Person();personType = xiaoming.GetType();Debug.Log(personType);//第三种:既没类也没类,又想获取类型。//通过静态方法GetType(),不推荐//需要填全名personType = Type.GetType("MyNamespace.Person");Debug.Log(personType);#endregion#region 了解Type属性//获取程序集/*Debug.Log(personType.Assembly.FullName);Debug.Log(typeof(GameObject).Assembly.FullName);//获取命名空间Debug.Log(personType.Namespace);Debug.Log(typeof(ClassClass).Namespace);//没有命名空间,就直接在程序集里//全名【命名空间+类名】Debug.Log(personType.FullName);//最全名【程序集+命名空间+类名】*/#endregion#region 获取成员、字段、属性、方法//获取成员/*MemberInfo[] memberInfos = typeof(GameObject).GetMembers(BindingFlags.Public| BindingFlags.Instance);for (int i = 0; i < memberInfos.Length; i++){Debug.Log(memberInfos[i].Name);}*///获取字段/*FieldInfo fieldInfo = typeof(GameObject).GetField("name",BindingFlags.Public);Debug.Log(fieldInfo);FieldInfo[] fieldInfos = personType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);for (int i = 0; i < fieldInfos.Length; i++){Debug.Log("<color = blue>" + fieldInfos[i].Name + "<color>");}*///修改字段//获取属性/*PropertyInfo[] propertyInfos = personType.GetProperties(BindingFlags.Public| BindingFlags.Instance);for (int i = 0; i < propertyInfos.Length; i++){Debug.Log("<color=lime>" + propertyInfos[i].Name + "<color>");propertyInfos[i].SetValue(null,4);}*///获取方法/*MethodInfo[] methodInfos = personType.GetMethods(BindingFlags.NonPublic| BindingFlags.Static);for (int i = 0; i < methodInfos.Length; i++){Debug.Log("<color = red>" + methodInfos[i].Name + "<color>");}//获取没有参数,没有返回值的方法//获取有参数的方法MethodInfo sayMathod = personType.GetMethod("Say", new[] {typeof(Person)});//调用sayMathod.Invoke(xiaoming, new []{ xiaoming });//获取私有方法MethodInfo sleepMathod = personType.GetMethod("Sleep", BindingFlags.NonPublic | BindingFlags.Instance,null,new [] {typeof(Person)},null);//调用sleepMathod.Invoke(xiaoming, new[] {xiaoming});//获取公有静态方法MethodInfo scpMathod = personType.GetMethod("Sleep", BindingFlags.Public | BindingFlags.Instance,null,new [] {typeof(Person)},null);sleepMathod.Invoke(xiaoming, new[] {"2021"});//获取私有静态方法MethodInfo scqMathod = personType.GetMethod("Sleep", BindingFlags.NonPublic | BindingFlags.Instance,null,new [] {typeof(Person)},null);sleepMathod.Invoke(xiaoming, new[] {"2021"});*/#endregion#region 反射方式实例化对象//实例化某个类型的对象,调用公共的无参的构造函数// Person akl = (Person)Activator.CreateInstance(typeof(Person));//实例化某个类型的对象,调用私有的无参的构造函数//Person akl = (Person)Activator.CreateInstance(typeof(Person),true);//实例化某个类型的对象,调用公有的有参的构造函数//实例化某个类型的对象,调用私有的有参的构造函数#endregion}
}