C# Attribute
Attribute 是C#非常重要的一块内容,需要研究一下。
Attribute 的简单使用:简而言之,就是可以自定义通用标志位,而不是在每个所需的类型中分别增加标志位。
//class专用attribute
[AttributeUsage(AttributeTargets.Class)]
public class TestAttribute : Attribute
{
public bool ReadFlag { get; set; } public bool WriteFlag { get; set; } public TestAttribute(){ } public TestAttribute(bool readFlag, bool writeFlag)
{
this.ReadFlag = readFlag;
this.WriteFlag = writeFlag;
}
} //property专用attribute
[AttributeUsage(AttributeTargets.Property)]
public class TestFieldAttribute : Attribute
{
public bool IsIgnore { get; set; } public TestFieldAttribute(){ } public TestFieldAttribute(bool isIgnore)
{
this.IsIgnore = isIgnore;
}
} //数字基类
public abstract class TestDemoBase
{
public abstract int Num { get; set; }
} //数字类一
[Test(true, true)] //使用时可以去掉"Attribute"后缀
public class TestDemoA : TestDemoBase
{
[TestField(IsIgnore = false)]
public override int Num { get; set; }
} //数字类二
[Test(true, false)]
public class TestDemoB : TestDemoBase
{
[TestField(IsIgnore = true)]
public override int Num { get; set; }
} //针对数字基类的打印接口
public static class Writer
{
public static void Output<T>(T writer) where T : TestDemoBase
{
var objectType = typeof(T); //typeof参数为类型而不是对象
var tagAttr = objectType.GetCustomAttribute(typeof(TestAttribute), false) as TestAttribute; //获取类的单个attribute
if(tagAttr.WriteFlag == true)
{
Console.WriteLine("Output:" + writer.Num.ToString()); //write标志为true则可以打印
}
else
{
var propertyInfo = objectType.GetProperty("Num");
foreach (var attribute in propertyInfo.GetCustomAttributes(false)) //获取所有attributes
{
if (attribute.GetType() == typeof(TestFieldAttribute))
{
if ((attribute as TestFieldAttribute).IsIgnore == true) //成员有ignore则可以忽略write标志
Console.WriteLine("Output:" + writer.Num.ToString());
}
}
}
}
} public class Program
{
static void Main(string[] args)
{
var demoA = new TestDemoA();
demoA.Num = ; var demoB = new TestDemoB();
demoB.Num = ; Writer.Output(demoA);
Writer.Output(demoB);
Console.Read();
}
}
看来马上要学习一下 C# 的反射原理了。
C# Attribute的更多相关文章
- [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute
剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...
- JavaScript特性(attribute)、属性(property)和样式(style)
最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...
- [C#] C# 知识回顾 - 特性 Attribute
C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...
- js attribute 和 jquery attr 方法
attribute 是原生js dom 对象上的一个属性,这个属性有很多子属性,比如 isId(判断属性是否是Id) , name (获取属性名称) , value (获取属性值),attribute ...
- 【.net 深呼吸】自定义特性(Attribute)的实现与检索方法
在.net的各个语言中,尤其是VB.NET和C#,都有特性这一东东,具体的概念,大家可以网上查,这里老周说一个非标准的概念——特性者,就是对象的附加数据.对象自然可以是类型.类型成员,以及程序集. 说 ...
- angular2系列教程(四)Attribute directives
今天我们要讲的是ng2的Attribute directives.顾名思义,就是操作dom属性的指令.这算是指令的第二课了,因为上节课的components实质也是指令. 例子
- 学会给你的类(及成员)来定制一套自己的Attribute吧
在通过Visual Studio创建的C#程序集中,都包含了一个AssemblyInfo.cs的文件,在这个文件中,我们常常会看到这样的代码 [assembly: AssemblyTitle(&quo ...
- Attribute操作的性能优化方式
Attribute是.NET平台上提供的一种元编程能力,可以通过标记的方式来修饰各种成员.无论是组件设计,语言之间互通,还是最普通的框架使 用,现在已经都离不开Attribute了.迫于Attribu ...
- SharePoint 2016 配置向导报错 - The 'ListInternal' attribute is not allowed
前言 配置SharePoint 2016的配置向导中,第三步创建配置数据库报错,然后百度.谷歌了一下,都没有解决,自己看日志搞定,也许会有人遇到类似问题,分享一下. 1.配置向导的错误截图,如下图: ...
- C# 知识特性 Attribute
C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...
随机推荐
- ArrayList、LinkedList、HashMap底层实现
ArrayList 底层的实现就是一个数组(固定大小),当数组长度不够用的时候就会重新开辟一个新的数组,然后将原来的数据拷贝到新的数组内. LinkedList 底层是一个链表,是由java实现的一个 ...
- python daemon化你的程序
在之前的树莓派网关项目中遇到了这样一个问题,由于要把网关写的Server持续运行,尤其是要加电自动开启.发现ssh登录开启服务程序之后,当把pty退出时Server端自动断开了,这里想到的APUE中第 ...
- 用crontab设置svn的定期更新任务
本以为用crontab设置svn的定期更新任务是件非常容易的事情,实践后方才知道,其实并不那么容易.设置例行性工作如下:0 8 * * * /usr/bin/svn up /data/test第二天, ...
- GNU Make----Core Automatic Variables
$@ 表示规则的目标文件名.如果目标是一个文档文件(Linux中,一般称.a 文件为 文档文件,也称为静态库文件),那么它代表这个文档的文件名.在多目标模式 规则中,它代表的是哪个触发规则被执行的目标 ...
- 【开源java游戏框架libgdx专题】-01-libgdx介绍
libgdx是一款开源的java游戏框架,而且还实现了Desktop/Android/BlackBerry/iOS/HTML5这些些平台的跨平台开发.官方网址:https://libgdx.badlo ...
- Unicode 与多字节编码
int _tmain(int argc, _TCHAR* argv[]) { //定义LPWSTR 类型的宽字符串 LPWSTR szUnicode = L"This is a Unicod ...
- Android打开系统的Document文档图片选择
打开Document UI 过滤图片 private void startAcitivty() { Intent intent = new Intent(); intent.setAction(&qu ...
- scrolView
禁止UIScrollView垂直方向滚动,只允许水平方向滚动 scrollview.contentSize = CGSizeMake(长度, 0); 禁止UIScrollView水平方向滚动,只允许 ...
- birt 运行环境搭建(部署到tomcat)
最近一直在研究eclipse的birt,各种坑~~~~(>_<)~~~~. Requirements:tomcat version:7.0,birt-runtime-4.6.0-20160 ...
- Maven Profile标签
Maven Profiles标签可以针对不同的环境来使用不同的配置文件 在发布的时候可以用 mvn release -p product mvn release -p test mvn release ...