You can use predefined attributes to attach metadata to type members.

You can also define a custom attribute by creating a new class inheriting from System.Attribute. The class name must end in "Attribute". You typically define a conostructor that takes arguments that consist of the metadata that you want to attach to the type membere.

 /// <summary>
/// Attach to a class method to indicate kg of methane that is
/// output when calling the method.
/// </summary>
public class MethaneFootprintAttribute : Attribute
{
public double kgMethane; public MethaneFootprintAttribute(int kg)
{
kgMethane = kg;
}
}

You can use the new attribute to attach metadata to individual type members. You use the name of the new class, without the trailing "Attribute".

 [MethaneFootprint()]
public void FeedCowInBarn()
{
Console.WriteLine("Cow eats slop in dim confines of barn");
} [MethaneFootprint()]
public void LetGrazeOutside()
{
Console.WriteLine("Cow enjoys grazing and ends up healthier");
}

原文地址:#470 Define Your Own Custom Attribute

【转载】#470 Define Your Own Custom Attribute的更多相关文章

  1. JSF使用HTML5的custom attribute

    只需要在页面引用这样的命名空间: xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"之后,在JSF的控件的html5属性加上前缀&quo ...

  2. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

  3. 在ASP.NET Core中实现自定义验证特性(Custom Validation Attribute)

    这是我们在实际ASP.NET Core项目中用到的,验证用户名中是否包含空格. 开始是这么实现的(继承ValidationAttribute,重写IsValid方法): public class No ...

  4. 转载 C#中敏捷开发规范

    转载原地址 http://www.cnblogs.com/weixing/archive/2012/03/05/2380492.html 1.命名规则和风格 Naming Conventions an ...

  5. [Android Memory] Android Lint简介(转载)

    英文原文:http://tools.android.com/tips/lint  参照文章:http://blog.csdn.net/thl789/article/details/8037473 转载 ...

  6. Navigation - How to define the structure of the navigation tree via the NavigationItemAttribute

    In the meantime, you should use the Model Editor to create such a navigation structure. There are se ...

  7. 【转载】Gradle学习 第八章:依赖管理基础

    转载地址:http://ask.android-studio.org/?/article/10 This chapter introduces some of the basics of depend ...

  8. 仅反射加载(ReflectionOnlyLoadFrom)的 .NET 程序集,如何反射获取它的 Attribute 元数据呢?

    原文:仅反射加载(ReflectionOnlyLoadFrom)的 .NET 程序集,如何反射获取它的 Attribute 元数据呢? 平时我们获取一个程序集或者类型的 Attribute 是非常轻松 ...

  9. [转]How do you create a custom AuthorizeAttribute in ASP.NET Core?

    问: I'm trying to make a custom authorization attribute in ASP.NET Core. In previous versions it was ...

随机推荐

  1. Yii2 执行Save()方法失败,却没有错误信息

    一般用$model->errors 就能查看到更新失败的原因,但是这次却什么错误信息都没有,最后发现是因为在模型类中定义了一个方法 public function beforeSave($ins ...

  2. mysql DCl语句

    DCl 语句主要书DBA用来管理系统中的对象权限使用 grant select,insert on sakila.* 'kingle'@'localhost' identified by '123'; ...

  3. 解决Input number 框能够能够输入eeeeee 的问题

    onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))" 在input  type="n ...

  4. 学习javscript对象笔记(一)

    对象(Objects)  对象属性值可以是除undefined值之外的任意值 1.对象字面量 a.{}  b.new object(); 注意事项: 在对象字面量中,如果属性名是一个合法的javasc ...

  5. 基于前端js模板替换的多语言方案思考

    最近在做将一个系统多语言化的项目,系统使用的是ASP.NET,直接使用了一种已有的方案:在页面渲染时采用正则表达式替换{XXX:001 确定}格式的标记.但是这个方式增加了服务端的字符串处理,对页面性 ...

  6. poi 多行合并

    poi做多行合并,一定需要先绘制单元格,然后写入数据,最后合并,不然各种坑啊. 合并单元格所使用的方法: sheet.addMergedRegion( CellRangeAddress  cellRa ...

  7. js数据类型检测小结

    在js中,有四种用于检测数据类型的方式,分别是: typeof 用来检测数据类型的运算符 instanceof 检测一个实例是否属于某个类 constructor 构造函数 Object.protot ...

  8. eclipse-java-style.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><profil ...

  9. Implementation: Quick Sort 2014-08-19

    #include <stdio.h> void print(int *a, int start , int end); void quick_sort(int *a, int start, ...

  10. javascript对HTML字符转义与反转义

    1.背景:在项目中,经常遇到一些字符需要进行转义后才能显示到界面上,如“&”,在界面中显示的是“&”,在html中书写“&”,显示在界面的中的依然是“&”. 这时候,就 ...