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

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

 

1.自定义特性类

复制代码
using System;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 自定义新特性
/// </summary>
[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<NetworkMsg>();
            //判断attribute是否为NetworkMsg
            if (attribute is NetworkMsg)
            {
                Debug.Log(oneClass.Name);
            }
        }
复制代码

 

posted @   三页菌  阅读(1020)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示