当前位置:网站首页>C # learning (advanced course) day13 - Reflection
C # learning (advanced course) day13 - Reflection
2022-06-23 12:38:00 【First wife ash yuanai】
4.6 Reflection Disassemble the class
1. Reflection ( Private , character string 、 Or features are used )
2. Namespace
3.Type
4. Access private BingdingFlags
5. member :
MemberInfo
6. Field :( Differentiate static , Not static )
FieldInfo
Public fields of the instance 、 The private field of the instance
Static public fields 、 Static private fields
getValue ,setvalue
7. understand Type Properties of :( Differentiate static , Not static )
ProPertyInfo
GetProperty(), GetProperties()
Public instance properties 、 Public static properties
Private instance properties 、 Private static properties
getValue,setvalue Get and assign
8. understand Type Methods
personType.getMethod() 、personType.getMethods()
MethodInfo
The default is public method
Public instance methods 、 Public static methods
Private instance method 、 Private static methods
Inherited from Object Four ways to do it :equals、gethashcode、gettype、tostring
Member method
(1) No parameter 、( No return value )、 Public
obtain :Methodinfo method =
call :
(2) With parameters 、( No return value )、 Public ( How to pass parameters in the face of overloading )
obtain :
call :
(3) With parameters 、 Private
obtain :personType
call :
Static methods
(1) Public :
(2) Private :
9. Instantiate an object in reflection mode
Call common
(1) Instantiate an object of a certain type , Call the public parameterless constructor \ Return to one Object
Activator.CreateInstance() //
Call private
(2) Instantiate an object of a certain type , Call the private parameterless constructor
Activator.CreateInstance( , nonPublic)
(3) Instantiate an object of a certain type , Call the private parameterized constructor
(4) Instantiate an object of a certain type , Call the public parameterized constructor
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using MyNamespace;
using UnityEngine;
public class LearnReflectionTest : MonoBehaviour
{
private void Start()
{
#region Access to type
// The first one is : Get the type of the class through the class
// adopt typeof obtain Person The type of the class
// Be careful :typeof The types in must be accessible
Type personType = typeof(Person);
Debug.Log(personType);
// The second kind : Get the type of the class through the object
// adopt GetType() Get the type of the class to which the object belongs
Person xiaoming = new Person();
personType = xiaoming.GetType();
Debug.Log(personType);
// The third kind of : There are neither classes nor classes , And want to get the type .
// By static method GetType(), Not recommended
// You need to fill in your full name
personType = Type.GetType("MyNamespace.Person");
Debug.Log(personType);
#endregion
#region understand Type attribute
// Get assembly
/*Debug.Log(personType.Assembly.FullName);
Debug.Log(typeof(GameObject).Assembly.FullName);
// Get namespace
Debug.Log(personType.Namespace);
Debug.Log(typeof(ClassClass).Namespace);// No namespace , Directly in the assembly
// full name 【 Namespace + Class name 】
Debug.Log(personType.FullName);
// Full name 【 Assembly + Namespace + Class name 】*/
#endregion
#region Get members 、 Field 、 attribute 、 Method
// Get members
/*MemberInfo[] memberInfos = typeof(GameObject).GetMembers(BindingFlags.Public
| BindingFlags.Instance);
for (int i = 0; i < memberInfos.Length; i++)
{
Debug.Log(memberInfos[i].Name);
}*/
// Get field
/*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>");
}*/
// Modify fields
// get attribute
/*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);
}*/
// Access method
/*MethodInfo[] methodInfos = personType.GetMethods(BindingFlags.NonPublic
| BindingFlags.Static);
for (int i = 0; i < methodInfos.Length; i++)
{
Debug.Log("<color = red>" + methodInfos[i].Name + "<color>");
}
// Get no parameters , There is no way to return a value
// Get the method with parameters
MethodInfo sayMathod = personType.GetMethod("Say", new[] {typeof(Person)});
// call
sayMathod.Invoke(xiaoming, new []{ xiaoming });
// Get private methods
MethodInfo sleepMathod = personType.GetMethod("Sleep", BindingFlags.NonPublic | BindingFlags.Instance,
null,new [] {typeof(Person)},null);
// call
sleepMathod.Invoke(xiaoming, new[] {xiaoming});
// Get public static methods
MethodInfo scpMathod = personType.GetMethod("Sleep", BindingFlags.Public | BindingFlags.Instance,
null,new [] {typeof(Person)},null);
sleepMathod.Invoke(xiaoming, new[] {"2021"});
// Get private static methods
MethodInfo scqMathod = personType.GetMethod("Sleep", BindingFlags.NonPublic | BindingFlags.Instance,
null,new [] {typeof(Person)},null);
sleepMathod.Invoke(xiaoming, new[] {"2021"});*/
#endregion
#region Instantiate objects in reflection mode
// Instantiate an object of a certain type , Call the public parameterless constructor
// Person akl = (Person)Activator.CreateInstance(typeof(Person));
// Instantiate an object of a certain type , Call the private parameterless constructor
//Person akl = (Person)Activator.CreateInstance(typeof(Person),true);
// Instantiate an object of a certain type , Call the public parameterized constructor
// Instantiate an object of a certain type , Call the private parameterized constructor
#endregion
}
}
边栏推荐
- Easy to understand soft route brushing tutorial
- C#学习(高级课程)Day13——反射
- HMS core video editing service has the ability to open templates, helping users get the same cool video with one click
- 手机开户有什么风险吗?开户安全吗?
- ROS observation [57]: configure arm robots to grasp things
- QT knowledge: QT widgets widget function [02]
- Machine Learning Series 5: distance space (1)
- ROS knowledge: reading point cloud data files
- IPSec传输模式下ESP报文的装包与拆包过程
- 二维激光SLAM( 使用Laser Scan Matcher )
猜你喜欢

ROS知识:点云文件.pcd格式

Blue Bridge Cup single chip microcomputer (I) -- turn off peripherals and turn off led

跟循泰国国内游宣传曲MV,像本地人一样游曼谷

Common fault analysis and Countermeasures of MySQL in go language

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

AssetBundle资源管理

Transformers are RNNs (linear transformer)论文阅读

QT knowledge: using the qgraphicspixmapitem class

DevEco Device Tool 助力OpenHarmony设备开发

Ecological Wanli database and Westone completed compatible certification to jointly build a network security ecosystem
随机推荐
Huawei cloud gaussdb heavily released HTAP for commercial use, defining a new paradigm of cloud native database 2.0
C# 文件下载方式
群晖万兆网络配置与测试
Runtime application self-protection (rasp): self-cultivation of application security
Want to learn ETS development? Teach you to develop an iq-eq test application
2022 construction worker - Equipment direction - post skill (construction worker) test question simulation test platform operation
这两所985大学,共享同一位校长!校方:属实
How to uninstall and reinstall gazebo
C#学习(高级课程)Day14——特性
Follow the promotional music MV of domestic tour in Thailand and travel to Bangkok like local people
kubernetes comfig subpath
Halcon principle: correlation matching
DuPont analysis: what is the investment value of Anyang Iron and Steel Co., Ltd?
Oracle数据库的主导地位被云竞争对手逐渐侵蚀
mysql innodb的redo log buffer中未commit的事务持久化到redo log后,万一事务rollback了怎么办?
如何卸载Gazebo与重装
Excel-VBA 快速上手(一、宏、VBA、过程、类型与变量、函数)
Qt知识:视图框架QGraphicsWidget详解
ROS observation [57]: configure arm robots to grasp things
ROS知识:读取点云数据文件