使用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 = ; 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#特性,给方法或类打自定义标签再反射获取的更多相关文章
- java通过反射获取调用变量以及方法
一:反射概念 可以通过Class类获取某个类的成员变量以及方法,并且调用之. 二:通过反射获取方法.变量.构造方法 @Test // 通过反射获取类定义的方法 public void testMeth ...
- C#反射 获取程序集信息和通过类名创建类实例(转载)
C#反射获取程序集信息和通过类名创建类实例 . System.Reflection 命名空间:包含通过检查托管代码中程序集.模块.成员.参数和其他实体的元数据来检索其相关信息的类型. Assembly ...
- 通过反射获取及调用方法(Method)
1.获取方法使用反射获取某一个类中的方法,步骤:①找到获取方法所在类的字节码对象②找到需要被获取的方法 Class类中常用方法: public Method[] getMethods():获取包括自身 ...
- .NET C#利用反射获取类文件以及其中的方法&属性 并获取类及方法上的特性
了解C#特性类并声明我们自己的特性类[AttributeTest]代码如下 using System; namespace AttributeTest { /* 特性说明 特性本质是一个继承和使用了系 ...
- Java SE学习笔记 --->高级类特性 ---> toString() 方法
概述: toString() 方法在面向对象当中十分常见,使用频率很高,和equals() 方法一样,也是Object类中定义的方法. jdk中 源码: java.lang.Object类中ToStr ...
- [C#]Attribute特性(2)——方法的特性及特性参数
上篇博文[C#]Attribute特性介绍了特性的定义,类的特性,字段的特性,这篇博文将介绍方法的特性及特性参数相关概念. 3.方法的特性 之所以将这部分单列出来进行讨论,是因为对方法的特性查询的反射 ...
- Android开发中,那些让您觉得相见恨晚的方法、类或接口
Android开发中,那些让你觉得相见恨晚的方法.类或接口本篇文章内容提取自知乎Android开发中,有哪些让你觉得相见恨晚的方法.类或接口?,其实有一部是JAVA的,但是在android开发中也算常 ...
- 如何在方法上贴上attribute(特性)捕捉方法的异常,来实现我们的需求
在方法上贴上attribute(特性)捕捉方法的异常,其实这么做也是为了在项目中不会大量使用try-cacth这样的语句,同时使我们的代码看起来更简洁,更直观,将逻辑业务分离使得后期维护方便.这里我们 ...
- 【前端基础系列】slice方法将类数组转换数组实现原理
问题描述 在日常编码中会遇到将类数组对象转换为数组的问题,其中常用到的一种方式使用Array.prototype.slice()方法. 类数组对象 所谓的类数组对象,JavaScript对它们定义为: ...
随机推荐
- You must restart adb and Eclipse.
打开Eclipse运行android 程序发现虚拟机启动不了提示 You must restart adb and Eclipse. 如下方式适用于端口占用的情况: 1.netstat -ano|f ...
- 系统数据库--修改tempdb的位置
use mastergoAlter database tempdb modify file (name = tempdev, filename = 'G:\db\tempdb.mdf')goAlter ...
- Backup--压缩备份和TDE
1>对启用TDE的数据库,压缩备份的备份文件大小与未压缩备份的备份文件大小差不多(压缩比为 1 ) 2>对启用TDE的数据库,压缩备份的备份时间远高于未压缩备份 2>对启用TDE的数 ...
- webstorm中新建vue工程
1.在https://nodejs.org/en/下载安装nodejs 2.vue-cli 搭建框架 首先从官方网站安装 node.js,会一并安装 npm工具.注意 npm一定要3.10以上,以免很 ...
- path的用法和所遇错误
首先上源代码: def _path(route, view, kwargs=None, name=None, Pattern=None): if isinstance(view, (list, tup ...
- linux杀进程
1.Linux杀进程的一个例子 (1)查看进程:ps -ef | grep 进程名 (2)杀进程:kill -9 进程名或者进程号 例如:杀tomcat的进程 (1)ps -ef | grep tom ...
- bzoj 3669: [Noi2014]魔法森林(并查集+LCT)
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...
- 使用vue+webpack从零搭建项目
vue到现在已经成为一个热门的框架,在项目实践当中,如果想要创建一个新项目,通常都会使用vue-cli的脚手架工具,毋容置疑能够方便很多,很多东西也不需要自己亲自去配置.都知道,脚手架其实是vue结合 ...
- 方法引用(Method reference)和invokedynamic指令详细分析
方法引用(Method reference)和invokedynamic指令详细分析 invokedynamic是jvm指令集里面最复杂的一条.本文将详细分析invokedynamic指令是如何实现方 ...
- 2016级算法第五次上机-B.Bamboo&APTX4844魔发药水
Bamboo&APTX4844魔发药水 题意 "于是,Bamboo耐着性子,看巫师从袖子里掏出 M 瓶时光泉水和 K 粒绿色能量.每瓶时光泉水重量为 c ,生发效果为 l:每粒绿色能 ...