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 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...
随机推荐
- 「书评」SAP内存计算——HANA
因为工作关系,长期跟SAP打交道,所以去年就对HANA有了一些了解,只是公司目前的应用规模还较小,暂时没有上马HANA的打算,但是提前作一些学习还是很有必要的.正好清华大学出版社最近出版了这本< ...
- 适配iOS7uinavigationbar遮挡tableView的问题
//适配iOS7uinavigationbar遮挡tableView的问题 if([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0 ...
- 强迫症和拖延症患者如何应对马桶4(遨游Maxthon)“上次未关闭页面”丢失的问题
强迫症和拖延症患者如何应对马桶4(遨游Maxthon)“上次未关闭页面”丢失的问题 用了马桶好多年,虽然一直bug不断,经常假死丢数据坑爹什么的,但是总得来说还是略有感情,不忍舍弃. 马桶一直有一个好 ...
- android ToolBar与DrawerLayout笔记
通过Android Studio 生成的Nagvition DrawerLayout Activity 自带的布局中的NagvitionView会覆盖ToolBar直接通到statusBar. 但是自 ...
- codevs 1200 同余方程 (Extend_Eulid)
/* 扩展欧几里得 ax%b==1 -> ax-by==1 求不定方程的一组解 使x为最小正整数解 */ #include<iostream> #include<cstdio& ...
- 学习CSS一些事(下)
2.浮动(float) 浮动(float)特点:1.元素会左移.右移,直到触碰到容器为止. 2.设置浮动元素,仍旧处于标准文档流. 3.当元素没有设置宽度值,而设置了浮动属性,元素的宽度随着内容 ...
- Python 时间函数
时间的运用 #coding=utf-8 #!user/bin/python import time import calendar ticks = time.asctime(time.localtim ...
- leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...
- jQuery ajax - serialize() 方法
http://www.jb51.net/article/60849.htm http://www.w3school.com.cn/jquery/ajax_serialize.asp
- shareSDK微博分享出现: 分享失败: 错误描述:Insufficient app permissions! 错误码:10014
这个错误是由于appKey所在账号没有微博高级写入接口权限, 需要申请, 详见: http://www.mamicode.com/info-detail-936938.html