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. F查询和Q查询

    F查询 在上面所有的例子中,我们构造的过滤器都只是将字段值与某个我们自己设定的常量做比较.如果我们要对两个字段的值做比较,那该怎么做呢? Django 提供 F() 来做这样的比较.F() 的实例可以 ...

  2. Flutter FormatException: Bad UTF-8 encoding 0xc3 (at offset 172)

    是文件编码问题. 1,打开 Android Studio 进入设置界面 Ctrl + Alt + S 或 File --> Settings 打开如下界面: 2,将 Project Encode ...

  3. my33_内存满导致mysqld被kill

    监控报警发现MGR的一个节点故障,查看时发现LVS已经发生切换,LVS切到了MGR新的写节点上了,排查原因 /var/log/message Mar :: db10 kernel: crond inv ...

  4. 3---Django rest framework源码分析(3)----节流

    Django rest framework源码分析(3)----节流 目录 添加节流 自定义节流的方法  限制60s内只能访问3次 (1)API文件夹下面新建throttle.py,代码如下: # u ...

  5. Apache Beam中的函数式编程理念

    不多说,直接上干货! Apache Beam中的函数式编程理念 Apache Beam的编程范式借鉴了函数式编程的概念,从工程和实现角度向命令式妥协. 编程的领域里有三大流派:函数式.命令式.逻辑式. ...

  6. VS2010中VC++目录和C/C++之间的区别。VC++ Directories和C/C++的区别。

    首先,这是个历史遗留问题,说起来比较复杂.其次,这个问题在微软的MSDN博客上已经专门被说起过了,英文好的请直接移步到原文:<VC++ Directories>.另外,stack over ...

  7. ubuntu-14.10 输入法切换设置

    1. 设置---系统设置 2. 语言支持---添加或删除语言---汉语 3. 关闭,系统设置找到 文本输入 4. 可以添加.删除,以及切换方式

  8. POJ 3468——A Simple Problem with Integers——————【线段树区间更新, 区间查询】

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 86780   ...

  9. [转]NLog学习笔记二:深入学习

    本文转自:http://www.cnblogs.com/CCHUncle/p/5207735.html 配置文件 NLog所有的配置信息都可以写到一个单独的xml文件中,也可以在程序代码中进行配置. ...

  10. sql server数据库打不开