使用c#特性,给方法或类打自定义标签再反射获取

给方法打自定义标签再反射获取

 

1.自定义特性类

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

/// 
/// 自定义新特性
/// 
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class CustomA : Attribute {

}

2.获取

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

public class MyTest : MonoBehaviour {

    // Use this for initialization
    void Start () {
        //反射自己这类
        Type t = GetType();
        //拿去本类的方法
        MethodInfo[] _method = t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
        for (int i = 0; i < _method.Length; i++)
        {
            System.Object[] _attrs = _method[i].GetCustomAttributes(typeof(CustomA), false);  //反射获得用户自定义属性
            var count = _attrs.Length;
            for (int j = 0; j < _attrs.Length; j++)
            {
                if (_attrs[j] is CustomA)
                {
                    Debug.Log("被注册的方法为:" + _method[i].Name);
                }
            }
        }
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    [CustomA]
    public void MySimpleMethod()
    {

    }
}

 

================================================================================================================================================================================================

 

【给类打上标签】获得相对于打上标签的类

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

public class Test1 : MonoBehaviour {

    // Use this for initialization
    void Start () {
        //获取程序集
        Assembly assembly = this.GetType().Assembly;
        //得到程序集的显示名称
        //Debug.Log(assembly.FullName);
        //通过程序集 得到程序集中的所有的类
        Type[] types = assembly.GetTypes();
        //遍历类
        foreach (Type item in types)
        {
            //获得打上CustomA标签的类(已设置 AttributeTargets.All)
            System.Object[] _attrs = item.GetCustomAttributes(typeof(CustomA), false);  //反射获得用户自定义属性
            foreach (var it in _attrs)
            {
                if (it is CustomA)
                {
                    Debug.Log("得到" + item.Name);
                }
            }
        }

        //打印所有类
        foreach (var type in types)
        {
            Debug.Log(type.Name);
        }
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

【打标签的类】

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

[CustomA]
public class Test2 : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

 

简写

        //获得当前类的程序集
        Assembly x = this.GetType().Assembly;
        //获取此程序集中的所有类
        Type[] allClass = x.GetTypes();
        //遍历操作
        foreach (Type oneClass in allClass)
        {
            //获取类的attribute
            var attribute = oneClass.GetCustomAttribute();
            //判断attribute是否为NetworkMsg
            if (attribute is NetworkMsg)
            {
                Debug.Log(oneClass.Name);
            }
        }

 

转载于:https://www.cnblogs.com/sanyejun/p/8981435.html

你可能感兴趣的:(使用c#特性,给方法或类打自定义标签再反射获取)