当前位置:网站首页>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
}
}
边栏推荐
- Slam Laser 2D (en utilisant Laser Scan matcher)
- 2022工具钳工(初级)考试练习题模拟考试平台操作
- Qt 知识:使用 QGraphicsPixmapItem类
- What are the risks of opening a mobile account? Is it safe to open an account?
- 2022施工员-装饰方向-岗位技能(施工员)操作证考试题库模拟考试平台操作
- 一个 BUG 开发表示用户不会这样操作,无需修复,测试人员如何应对?
- 冷板式、浸没式、喷淋式液冷散热能否引领高性能计算发展?
- 2D laser Slam (using laser scan matcher)
- record
- 【系统架构】-软件架构的5大风格
猜你喜欢

Explain the relationship between pyqt5 signal and slot in detail

Stimulsoft Ultimate Reports 2022.3.1

halcon原理:相关性匹配

唐人街徒步:在异国情调的纽约感受浓厚的中式气息

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

SQL adds the problem of duplicate table records.

Basic data type and corresponding packing class

二維激光SLAM( 使用Laser Scan Matcher )

安装Rstudio Desktop和Rstudio Server免费版本

ROS2知识(2):网络设施
随机推荐
Ecological Wanli database and Westone completed compatible certification to jointly build a network security ecosystem
解决“Thread 1: “-[*.CollectionNormalCellView isSelected]: unrecognized selector sent to instance 0x7f”
sql增加表记录的重复问题。
二維激光SLAM( 使用Laser Scan Matcher )
Localization information | aikesheng and China kefangde complete product compatibility and mutual certification
SQL adds the problem of duplicate table records.
Linked list 5 - 234 Palindrome linked list
[basic knowledge] - data bit width converter
ROS2知识(6):坐标对象TF原理和实践
2022施工员-装饰方向-岗位技能(施工员)操作证考试题库模拟考试平台操作
Solve "thread 1:" -[*.collectionnormalcellview isselected]: unrecognized selector sent to instance 0x7F "
09 -- palindrome pair
CSS magic nugget mug; Optimization of CK, ES and redisearch schemes in ten million level data query; Why does module circular dependency not lead to dead loop Sauced afternoon tea issue 13
ROS知识:点云文件.pcd格式
唐人街徒步:在异国情调的纽约感受浓厚的中式气息
The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year
2022 construction worker - decoration direction - post skills (construction worker) operation certificate examination question bank simulated examination platform operation
C#学习(高级课程)Day14——特性
解决:Argument type ‘String‘ expected to be an instance of a class or class-constrained type
ROS察微【57】:配置手臂机器人来抓东西