当前位置:网站首页>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
}
}
边栏推荐
- HMS Core 视频编辑服务开放模板能力,助力用户一键Get同款酷炫视频
- 『忘了再学』Shell流程控制 — 39、特殊流程控制语句
- QT knowledge: detailed explanation of view frame qgraphicswidget
- Slam Laser 2D (en utilisant Laser Scan matcher)
- CDH邮件报警配置
- 【无标题】2022年压力管道巡检维护试题及在线模拟考试
- 10-- 根据中序遍历和后序遍历,构造二叉树
- Use xtradiagram Diagramcontrol for drawing and controlling process graphics
- Go 语言使用 MySQL 的常见故障分析和应对方法
- [cloud native & microservice viii] source code analysis of weightedresponsetimerule of ribbon load balancing strategy (response time weighting)
猜你喜欢

Slam Laser 2D (en utilisant Laser Scan matcher)

Redis 入门-第四篇-数据结构与对象-跳跃表

Machine Learning Series 5: distance space (1)

Wallys/DR6018-S/ 802.11AX MU-MIMO OFDMA / 2* GE PORTS/WIFI 6e / BAND DUAL CONCURRENT

Deveco device tool helps openharmony device development

Ros2 knowledge (1): start practicing robots

The median annual salary exceeds 300000, and the salary of the first AI major graduate of Nanjing University is exposed

华为云GaussDB重磅发布HTAP商用,定义云原生数据库2.0新范式

@Dark horse fans, haven't you received this "high temperature subsidy"?

数据中台稳定性的“四高” | StartDT Tech Lab 18
随机推荐
Three ways to learn at work
ROS察微【51】:如何将里程计和 IMU 与 robots_localization 融合
Shell process control - 39. Special process control statements
理财产品的赎回时间有规定吗?
Leetcode 1209. Delete all adjacent duplicates II in the string
Getting started with redis - Chapter 4 - data structures and objects - jump table
Qt知识:视图框架QGraphicsWidget详解
Halcon knowledge: binocular_ Discrimination knowledge
2022 construction worker - decoration direction - post skills (construction worker) operation certificate examination question bank simulated examination platform operation
halcon原理:一维函数function_1d类型【2】
Design of routing service for multi Activity Architecture Design
Want to learn ETS development? Teach you to develop an iq-eq test application
安装Rstudio Desktop和Rstudio Server免费版本
mysql中innodb下的redo log什么时候开始执行check point落盘的?
Install rstudio desktop and rstudio server free version
The country has entered the main flood season. The Ministry of transport: the lines that do not meet the conditions for safe operation will be resolutely shut down!
[comprehensive written test questions] 30 Concatenate substrings of all words
10-- construct binary tree according to middle order traversal and post order traversal
Meta 称英国安全法将“扫描所有私人信息”,有侵犯用户隐私风险
ROS2知识(6):坐标对象TF原理和实践