C#---反射

【千锋合集】史上最全Unity3D全套教程|匠心之作_哔哩哔哩_bilibili

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UIFrame;

namespace TankGame {
    public class Tank {


    }
    public class Bullet {

    }
    public class Enemy {

    }
}

public class ReflectionType : MonoBehaviour {

    private void Start()
    {
        #region 获取类型-Type
        通过类名获取类别的方式 typeof
        //Type personType = typeof(Person);
        //Debug.Log("personType:"+personType);
        //Type tankType = typeof(TankGame.Tank);
        //Type gameObjectType = typeof(GameObject);
        通过对象,获取类别GetType()
        //Type transformType = transform.GetType();
        有一个炮弹对象
        //TankGame.Bullet blt = new TankGame.Bullet();
        //Type BulletType = blt.GetType();
        通过类名的字符串,获取类别 static GetType()
        //Type enemyType=Type.GetType("TankGame.Enemy");
        //Type rigidBodyType = Type.GetType("UnityEngine.Rigidbody");
        #endregion

        #region Type类中的属性
        Type goType = typeof(GameObject);
        Debug.Log(goType.Name);
        Debug.Log(goType.FullName);
        //程序集
        Debug.Log(goType.Assembly);
        //程序集+命名空间+类名
        Debug.Log(goType.AssemblyQualifiedName);
        //获取父类
        Debug.Log(goType.BaseType);
        gameObject.AddComponent(typeof(Rigidbody));
        #endregion
        //添加一个脚本组件,但限制该组件必须继承后面的那个类
        AddComponentWithInhert("HeroObjBehaviour", "ObjBehaviourBase");
    }
    public bool IsInhert(string sonAssemblyQualifiedName, string parentAssemblyQualifiedName) {
        Type sonType = Type.GetType(sonAssemblyQualifiedName);
        Type parentType = Type.GetType(parentAssemblyQualifiedName);
        //判断两个类是否为父子关系
        if (sonType.BaseType == parentType) {
            return true;
        }
        return false;
    }
    public void AddComponentWithInhert(string sonAssemblyQualifiedName, string parentAssemblyQualifiedName) {
        bool isInhert=IsInhert(sonAssemblyQualifiedName, parentAssemblyQualifiedName);
        if (isInhert) {
            gameObject.AddComponent(Type.GetType(sonAssemblyQualifiedName));

        }
    }
}
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjBehaviourBase : MonoBehaviour {

    
}
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeroObjBehaviour : ObjBehaviourBase
{

}
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface IBehaviour {
    void ShowMe();
}
public class ObjBehaviourBase : MonoBehaviour,IBehaviour {
    public ObjBehaviourBase() { }
    private ObjBehaviourBase(string obnName) {
        this.objName = objName;
    }


    //字段
    private string objName;
    public float objLiftTime=666;
    
    //属性
    public string ObjName {
        get {
            return objName;
        }
        set {
            objName = value;
        }
    }
    public void ShowMe() {
        Debug.Log("ObjName:"+objName);
    }
    
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UIFrame;
using System.Reflection;

namespace TankGame {
    public class Tank {


    }
    public class Bullet {

    }
    public class Enemy {

    }
}

public class ReflectionType : MonoBehaviour {

    
    private void Start()
    {
        #region 获取类型-Type
        通过类名获取类别的方式 typeof
        //Type personType = typeof(Person);
        //Debug.Log("personType:"+personType);
        //Type tankType = typeof(TankGame.Tank);
        //Type gameObjectType = typeof(GameObject);
        通过对象,获取类别GetType()
        //Type transformType = transform.GetType();
        有一个炮弹对象
        //TankGame.Bullet blt = new TankGame.Bullet();
        //Type BulletType = blt.GetType();
        通过类名的字符串,获取类别 static GetType()
        //Type enemyType=Type.GetType("TankGame.Enemy");
        //Type rigidBodyType = Type.GetType("UnityEngine.Rigidbody");
        #endregion

        #region Type类中的属性
        Type goType = typeof(GameObject);
        Debug.Log(goType.Name);
        Debug.Log(goType.FullName);
        //程序集
        Debug.Log(goType.Assembly);
        //程序集+命名空间+类名
        Debug.Log(goType.AssemblyQualifiedName);
        //获取父类
        Debug.Log(goType.BaseType);
        gameObject.AddComponent(typeof(Rigidbody));
        #endregion
        #region Type类中方法
        //获取类型
        Type objType = typeof(ObjBehaviourBase);
        MemberInfo[] objMemberInfos = objType.GetMembers();
        //for (int i=0;i<objMemberInfos.Length;i++) {
        //    Debug.Log(string.Format("MemberType[{0}]",i)+objMemberInfos[i].MemberType);
        //}
        //获取该类的所有构造函数
        ConstructorInfo[] ctors = objType.GetConstructors();
        for (int i = 0; i < ctors.Length; i++) {
            Debug.Log(ctors[i].IsPrivate);           
        }
        //获取公共字段
        FieldInfo ltField=objType.GetField("objLiftTime");
        Debug.Log("FieldType:" + ltField.FieldType);
        Debug.Log("FieldType:" + ltField.IsStatic);

        //获取私有成员字段
        FieldInfo[] fields=objType.GetFields(BindingFlags.Instance|BindingFlags.NonPublic);
        Debug.Log(fields[0].Name);
        //获取私有的成员字段
        FieldInfo[] gameObjectFields=typeof(GameObject).GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
        //遍历所有私有成员
        for (int i = 0; i < gameObjectFields.Length; i++) {
            Debug.Log("<color=lime>"+gameObjectFields[i].Name+"</color>");

        }

        //获取私有成员属性        
        PropertyInfo[] gameObjectProperty=typeof(GameObject).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic);
        //遍历所有私有成员
        for (int i = 0; i < gameObjectProperty.Length; i++) {
            Debug.Log("<color=lime>"+ gameObjectProperty[i].Name+"</color>");

        }

        //获取方法
        objType.GetMethods();
        MethodInfo[] methodInfos=typeof(GameObject).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
        for (int i = 0; i < methodInfos.Length; i++)
        {
            Debug.Log("<color=red>" + methodInfos[i].Name + "</color>");

        }

        //BindingFlags:表示搜索的按位组合
        //Public 16 or NonPublic 32
        //0001 0000    0010 0000
        //Static 8 or Instance 4
        //0000 1000    0000 0100
        //组合:私有的且非静态NonPublic Instance
        //NonPublic|Instance
        //  0010 0000
        //| 0000 0100
        //-----------------------
        //  0010 0100

        #endregion

        //添加一个脚本组件,但限制该组件必须继承后面的那个类
        AddComponentWithInhert("HeroObjBehaviour", "ObjBehaviourBase");
    }
    public bool IsInhert(string sonAssemblyQualifiedName, string parentAssemblyQualifiedName) {
        Type sonType = Type.GetType(sonAssemblyQualifiedName);
        Type parentType = Type.GetType(parentAssemblyQualifiedName);
        //判断两个类是否为父子关系
        if (sonType.BaseType == parentType) {
            return true;
        }
        return false;
    }
    public void AddComponentWithInhert(string sonAssemblyQualifiedName, string parentAssemblyQualifiedName) {
        bool isInhert=IsInhert(sonAssemblyQualifiedName, parentAssemblyQualifiedName);
        if (isInhert) {
            gameObject.AddComponent(Type.GetType(sonAssemblyQualifiedName));

        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值