当前位置:网站首页>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


    }
}
原网站

版权声明
本文为[First wife ash yuanai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231202151143.html