当前位置:网站首页>C#学习(高级课程)Day13——反射
C#学习(高级课程)Day13——反射
2022-06-23 12:02:00 【大老婆灰原哀】
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
}
}
边栏推荐
- Mysql, how to calculate the maximum value using stored procedures
- An idea of using keep alive to cache data in vue3 form pages
- Redis 入门-第三篇-数据结构与对象-字典
- 股权转让热点:重庆建科建设工程质量检测有限公司93.75%股权转让
- The median annual salary exceeds 300000, and the salary of the first AI major graduate of Nanjing University is exposed
- TT-SLAM:用于平面环境的密集单目SLAM(IEEE 2021)
- Voice module: pyttsx sound change project
- Go 语言使用 MySQL 的常见故障分析和应对方法
- Halcon知识:binocular_disparity 知识
- Common fault analysis and Countermeasures of MySQL in go language
猜你喜欢

Ppt makes 3D rotation animation from beginner to advanced

Halcon knowledge: binocular_ Discrimination knowledge

Open classes are short videos! Tonight, I will teach you how to realize accurately!

HMS Core 视频编辑服务开放模板能力,助力用户一键Get同款酷炫视频
![Halcon principle: one dimensional function_ 1D type [2]](/img/54/570c6e739be1ab9caa9df805965b57.png)
Halcon principle: one dimensional function_ 1D type [2]

“梦想童行” 2022年广汽本田儿童道路安全公益行走进东北

Localization information | aikesheng and China kefangde complete product compatibility and mutual certification

Oracle database's dominant position is gradually eroded by cloud competitors

Ros2 knowledge (6): principle and practice of coordinate object TF

蓝桥杯单片机(一)——关闭外设及熄灭LED
随机推荐
mysql innodb的redo log buffer中未commit的事务持久化到redo log后,万一事务rollback了怎么办?
Learning notes sweep crawler framework
运行时应用自我保护(RASP):应用安全的自我修养
ROS knowledge: the structure of the rviz library librviz
MySQL matches multiple values in one field
首次曝光!唯一全域最高等级背后的阿里云云原生安全全景图
10-- construct binary tree according to middle order traversal and post order traversal
Want to learn ETS development? Teach you to develop an iq-eq test application
Machine Learning Series 5: distance space (1)
CRMEB知识付费如何二开阿里云短信功能
Hot spot of equity transfer: 93.75% equity transfer of Chongqing Jianke Construction Engineering Quality Inspection Co., Ltd
WC statistics are out of date, and every line of cloc code is valid
halcon原理:一维函数function_1d类型【1】
Ppt makes 3D rotation animation from beginner to advanced
HMS Core 视频编辑服务开放模板能力,助力用户一键Get同款酷炫视频
Mysql, how to calculate the maximum value using stored procedures
Introduction to redis - Chapter 1 - data structures and objects - simple dynamic string (SDS)
ROS知识:rviz库librviz的结构
详解PyQt5信号与槽的关系
数据中台稳定性的“四高” | StartDT Tech Lab 18