在我们的项目中有时经常会标识一些类的特性,在下面我们将简短的方式来介绍如何构建自定义的Attribute类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks; namespace DLPVideoApp.DLPCtrl
{
// 摘要:
// 定义宿主程序的识别码。
[ComVisible(true)]
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public sealed class MyClassAttribute : Attribute
{
private string _identify = "";
// 摘要:
// 初始化 System.Reflection.AssemblyTitleAttribute 类的新实例。
//
// 参数:
// identifyNumber:
// 识别码。
public MyClassAttribute(string identifyType)
{
_identify = identifyType;
} public string IdentifyType
{
get
{
return _identify;
}
}
}
}

 在定义完一个继承自Attribute的基类之后,接下来就是如何去应用我们自定义的MyClassAttribute类了,其实也非常简单,我们只需要在要标明类属性的上面添加下面的内容即可:[MyClassAttribute("DLPVideoApp.DLPCtrl.DLPWin")](这里仅仅是举出一个案例)

接下来就是如何去应用该Attribute了,下面通过反射的方式来获取程序集中所有的类相关信息,然后再来匹配名称为[MyClassAttribute("DLPVideoApp.DLPCtrl.DLPWin")]的类。

Assembly exeAssembly = Assembly.GetExecutingAssembly();
Type[] types = exeAssembly.GetExportedTypes();
Type winType = null;
for (int i = 0; i <= types.Length - 1; i++)
{
Type t = types[i];
if (!(t.IsPublic && t.IsClass == true && t.IsAbstract == false)) continue; object[] clsAttrs = t.GetCustomAttributes(typeof(MyClassAttribute), false);
if (clsAttrs == null || clsAttrs.Length == 0) continue; MyClassAttribute myClassAtribute = clsAttrs[0] as MyClassAttribute;
if (myClassAtribute.IdentifyType == "DLPVideoApp.DLPCtrl.DLPWin")
{
winType = t;
break;
}
}

  Assembly exeAssembly = Assembly.GetExecutingAssembly(); Type[] types = exeAssembly.GetExportedTypes();通过这种方式能够获取一个程序集中所有的类以及事件,通过遍历Type数组来寻找自定义的Attribute类,这种方式有时还是非常有效的。另外还要记得去反思怎样正确使用类的属性。

自定义Attribute类的更多相关文章

  1. 转:C#制作ORM映射学习笔记一 自定义Attribute类

    之前在做unity项目时发现只能用odbc连接数据库,感觉非常的麻烦,因为之前做web开发的时候用惯了ORM映射,所以我想在unity中也用一下ORM(虽然我知道出于性能的考虑这样做事不好的,不过自己 ...

  2. XsdGen:通过自定义Attribute与反射自动生成XSD

    前言 系统之间的数据交互往往需要事先定义一些契约,在WCF中我们需要先编写XSD文件,然后通过自动代码生成工具自动生成C#对象.对于刚刚接触契约的人来说,掌握XMLSpy之类的软件之后确实比手写XML ...

  3. 2.C#自定义Attribute

    阅读目录    一:C#自定义Attribute    二:AttributeUsageAttribute中的3个属性(Property)中的AttributeTargets   三:Attribut ...

  4. 自定义Attribute 服务端校验 客户端校验

    MVC 自定义Attribute 服务端校验 客户端校验/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Autho ...

  5. C#自定义Attribute值的获取与优化

    C#自定义Attribute值的获取是开发中会经常用到的,一般我们的做法也就是用反射进行获取的,代码也不是很复杂. 1.首先有如下自定义的Attribute [AttributeUsage(Attri ...

  6. .net c#获取自定义Attribute

    前言: 在c#开发中,有时候我们需要读取 Attribute中的信息(关于Attribute , 我自己把他理解成一个可以为类,属性标记的东西,这个标记可以为你提供一些关于类,方法,属性的额外信息) ...

  7. CVS导出&&自定义Attribute的使用

    1.cvs导出:List转为byte[] /// <summary> /// CvsExport帮助类 /// </summary> public static class C ...

  8. 【MVC 笔记】MVC 自定义 Attribute 属性中的猫腻

    原想在 MVC Action 上加一个自定义 Attribute 来做一些控制操作,最先的做法是在自定 Attribute 中定义一个属性来做逻辑判断,可惜事与愿违,这个属性值居然会被缓存起来,于是于 ...

  9. java自定义注解类

    一.前言 今天阅读帆哥代码的时候,看到了之前没有见过的新东西, 比如java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思? 于是乎,学习并整理了一下. 二.代码示例 import ...

随机推荐

  1. P1824 进击的奶牛(二分)

    思路:把检验的函数说一下,就是检测的距离x时,是否存在c个隔断相离大于等于x,如果是则返回1,不是则返回0 #include<iostream> #include<cstdio> ...

  2. 【vue】iView-admin后台管理系统

    1.目录手册解析 2.跨域解决(axios访问服务器由于前端地址和后端地址不同源会出现跨域问题) 实例2 3.动态菜单导航+权限 4.webstorm 配置 Eslint 代码检查和自动修复

  3. 【vue】vue-router跳转路径url多种格式

    1.形如  http://localhost:8080/#/book?id=**** ①路由配置 ②路由定向链接,以query传参id 另外,获取query传递的参数id用  this.$route. ...

  4. Tencent Cloud Developers Conference(2018.12.15)

    时间:2018.12.15地点:北京朝阳悠唐皇冠假日酒店

  5. 剑指Offer-- 二叉搜索树中和为某一值的路径

    输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 本身题目不是很难,但是因为刚接触pyhon,对一些对象的传 ...

  6. NYOJ-16-矩形嵌套 记忆化搜索

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  7. poj2104 主席树裸题

    空间大小:n*lgn 复杂度:建树n*lgn  查询lgn #include <cstdio> #include <iostream> #include <algorit ...

  8. [Offer收割]编程练习赛97

    链接 [https://hihocoder.com/contest/offers97/problems] 题意 题目1 : 放置矩形 时间限制:10000ms 单点时限:1000ms 内存限制:256 ...

  9. c++入门之—运算符重载和友元函数

    运算符重载的意义是:将常见的运算符重载出其他的含义:比如将*重载出指针的含义,将<<与cout联合使用重载出输出的含义,但需要认识到的问题是:运算符的重载:本质仍然是成员函数,即你可以认为 ...

  10. rabbitmq集群运维一点总结

    说明:以下操作都以三节点集群为例,机器名标记为机器A.机器B.机器C,如果为双节点忽略机器C,如果为各多节点则与机器C操作相同 一.rabbitmq集群必要条件 1.1.绑定实体ip,即ip a所能查 ...