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的更多相关文章

  1. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  2. JavaScript特性(attribute)、属性(property)和样式(style)

    最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...

  3. [C#] C# 知识回顾 - 特性 Attribute

    C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...

  4. js attribute 和 jquery attr 方法

    attribute 是原生js dom 对象上的一个属性,这个属性有很多子属性,比如 isId(判断属性是否是Id) , name (获取属性名称) , value (获取属性值),attribute ...

  5. 【.net 深呼吸】自定义特性(Attribute)的实现与检索方法

    在.net的各个语言中,尤其是VB.NET和C#,都有特性这一东东,具体的概念,大家可以网上查,这里老周说一个非标准的概念——特性者,就是对象的附加数据.对象自然可以是类型.类型成员,以及程序集. 说 ...

  6. angular2系列教程(四)Attribute directives

    今天我们要讲的是ng2的Attribute directives.顾名思义,就是操作dom属性的指令.这算是指令的第二课了,因为上节课的components实质也是指令. 例子

  7. 学会给你的类(及成员)来定制一套自己的Attribute吧

    在通过Visual Studio创建的C#程序集中,都包含了一个AssemblyInfo.cs的文件,在这个文件中,我们常常会看到这样的代码 [assembly: AssemblyTitle(&quo ...

  8. Attribute操作的性能优化方式

    Attribute是.NET平台上提供的一种元编程能力,可以通过标记的方式来修饰各种成员.无论是组件设计,语言之间互通,还是最普通的框架使 用,现在已经都离不开Attribute了.迫于Attribu ...

  9. SharePoint 2016 配置向导报错 - The 'ListInternal' attribute is not allowed

    前言 配置SharePoint 2016的配置向导中,第三步创建配置数据库报错,然后百度.谷歌了一下,都没有解决,自己看日志搞定,也许会有人遇到类似问题,分享一下. 1.配置向导的错误截图,如下图: ...

  10. C# 知识特性 Attribute

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...

随机推荐

  1. OpenWrt简要刷机教程

    准备工作 1. 下载openwrt中文固件到PC.(当然其他英文固件也可) 2  找到路由器的RST键. 3  找到路由器刷机口---姑且称之为“WAN口” 4. 关闭路由器的电源. 5. 将PC网口 ...

  2. Virtualization API之libvirt

    The virtualization API 之开源 libvirt探究 By Ruiy: libvirt supports Hypervisors(注,相关的hypervisors项目的权威网站已经 ...

  3. poj1013

    题目大意:假造的银币 Sally Jones有一些游客给的银币,但是只有11枚是真正的银币(有一枚是假的),从颜色和大小是无法区分真比还是假币的,但是它的重量和真币是不同的,Sally Jones它是 ...

  4. 如何让自己的类用 copy 修饰符?如何重写带 copy 关键字的 setter?

    出题者简介: 孙源(sunnyxx),目前就职于百度 整理者简介:陈奕龙,目前就职于滴滴出行. 转载者:豆电雨(starain)微信:doudianyu 若想令自己所写的对象具有拷贝功能,则需实现 N ...

  5. Genymotion开始搞起~

    简介 一:什么是GenymotionGenymotion是一款完全超越BlueStacks的安卓模拟器,正如它中文官网的介绍:快到极致的Android模拟器.英文官网:http://www.genym ...

  6. eclipse+axis2+webservice开发实例

    myeclipse10安装axis2插件 第一步:下载axis2-1.6的插件压缩包,axis2-eclipse-codegen-plugin-1.6.2.zip 和 axis2-eclipse-se ...

  7. @property的特性

    @property还有一些关键字,它们都是有特殊作用的,比如上述代码中的nonatomic,strong: 1 2 @property(nonatomic,strong) NSString *carN ...

  8. is not in the sudoers file.This incident will be reported

    解决方法如下: 1>.进入超级用户模式.也就是输入"su -",系统会让你输入超级用户密码,输入密码后就进入了超级用户模式. 2>.添加文件的写权限.也就是输入命令&q ...

  9. Linux 套接字编程中要注意的细节

    隐患 1.忽略返回状态 第一个隐患很明显,但它是开发新手最容易犯的一个错误.如果您忽略函数的返回状态,当它们失败或部分成功的时候,您也许会迷失.反过来,这可能传播错误,使定位问题的源头变得困难. 捕获 ...

  10. 七.生成n位随机字符串

    --1.借助newid() go --创建视图(因为在函数中无法直接使用newid()) create view vnewid as select newid() N'MacoId'; go --创建 ...