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

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 = ; i < _method.Length; i++)
{
System.Object[] _attrs = _method[i].GetCustomAttributes(typeof(CustomA), false); //反射获得用户自定义属性
var count = _attrs.Length;
for (int j = ; 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);
}
}

使用c#特性,给方法或类打自定义标签再反射获取的更多相关文章

  1. java通过反射获取调用变量以及方法

    一:反射概念 可以通过Class类获取某个类的成员变量以及方法,并且调用之. 二:通过反射获取方法.变量.构造方法 @Test // 通过反射获取类定义的方法 public void testMeth ...

  2. C#反射 获取程序集信息和通过类名创建类实例(转载)

    C#反射获取程序集信息和通过类名创建类实例 . System.Reflection 命名空间:包含通过检查托管代码中程序集.模块.成员.参数和其他实体的元数据来检索其相关信息的类型. Assembly ...

  3. 通过反射获取及调用方法(Method)

    1.获取方法使用反射获取某一个类中的方法,步骤:①找到获取方法所在类的字节码对象②找到需要被获取的方法 Class类中常用方法: public Method[] getMethods():获取包括自身 ...

  4. .NET C#利用反射获取类文件以及其中的方法&属性 并获取类及方法上的特性

    了解C#特性类并声明我们自己的特性类[AttributeTest]代码如下 using System; namespace AttributeTest { /* 特性说明 特性本质是一个继承和使用了系 ...

  5. Java SE学习笔记 --->高级类特性 ---> toString() 方法

    概述: toString() 方法在面向对象当中十分常见,使用频率很高,和equals() 方法一样,也是Object类中定义的方法. jdk中 源码: java.lang.Object类中ToStr ...

  6. [C#]Attribute特性(2)——方法的特性及特性参数

    上篇博文[C#]Attribute特性介绍了特性的定义,类的特性,字段的特性,这篇博文将介绍方法的特性及特性参数相关概念. 3.方法的特性 之所以将这部分单列出来进行讨论,是因为对方法的特性查询的反射 ...

  7. Android开发中,那些让您觉得相见恨晚的方法、类或接口

    Android开发中,那些让你觉得相见恨晚的方法.类或接口本篇文章内容提取自知乎Android开发中,有哪些让你觉得相见恨晚的方法.类或接口?,其实有一部是JAVA的,但是在android开发中也算常 ...

  8. 如何在方法上贴上attribute(特性)捕捉方法的异常,来实现我们的需求

    在方法上贴上attribute(特性)捕捉方法的异常,其实这么做也是为了在项目中不会大量使用try-cacth这样的语句,同时使我们的代码看起来更简洁,更直观,将逻辑业务分离使得后期维护方便.这里我们 ...

  9. 【前端基础系列】slice方法将类数组转换数组实现原理

    问题描述 在日常编码中会遇到将类数组对象转换为数组的问题,其中常用到的一种方式使用Array.prototype.slice()方法. 类数组对象 所谓的类数组对象,JavaScript对它们定义为: ...

随机推荐

  1. Jessica's Reading Problem——POJ3320

    Jessica's Reading Problem——POJ3320 题目大意: Jessica 将面临考试,她只能临时抱佛脚的在短时间内将课本内的所有知识点过一轮,课本里面的P个知识点顺序混乱,而且 ...

  2. 10、Semantic-UI之图片的使用

    10.1 图片的使用 定义有边框的图片样式 <img class="ui medium bordered image" src="../images/pic.png ...

  3. Delphi XE8 iOS与Android移动应用开发(APP开发)教程[完整中文版]

    https://item.taobao.com/item.htm?id=536584650957&toSite=main

  4. .net 线程基础 ThreadPool 线程池

    1. ThreadPool 线程池异步: //public static bool QueueUserWorkItem(WaitCallback callBack); //public static ...

  5. Android-ContentProvider读取/新增/操作系统联系人数据

    想要访问Android操作系统的ContentProvider就需要明白以下原理: 在Android操作系统里面的 /packsges/目录: apps: 很多的系统应用,例如:联系人,浏览器,音乐播 ...

  6. heartbeat+nginx搭建高可用HA集群

    前言: HA即(high available)高可用,又被叫做双机热备,用于关键性业务.简单理解就是,有2台机器 A 和 B,正常是 A 提供服务,B 待命闲置,当 A 宕机或服务宕掉,会切换至B机器 ...

  7. [LeetCode 题解]: Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. 微软编程一小时 题目2: Longest Repeated Sequence

    题目2 : Longest Repeated Sequence 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a sequence of i ...

  9. element-ui学习

    1. 小技巧 使用导航路由 配置el-menu-item的index属性,如照片 也可使用数据动态配置导航菜单:参考 走马灯高度自适应 动态设置Carousel的height参数

  10. nginx location 与 rewrite详解 (转)

    点我