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. Oracle Merge 使用

    <转自> http://blog.csdn.net/nsj820/article/details/5755685 Oracle9i引入了MERGE命令,你能够在一个SQL语句中对一个表同时 ...

  2. 网络知识之ipset

    ipset介绍 ipset是iptables的扩展,它允许你创建 匹配整个地址集合的规则.而不像普通的iptables链只能单IP匹配, ip集合存储在带索引的数据结构中,这种结构即时集合比较大也可以 ...

  3. 控制台之console

    控制台中的用法有很多,比如常用的console.log(),还有不常用的 console.warn(). console.error()等,下面对控制台中主要的console方法做一个简单的介绍. 1 ...

  4. [Scala] Currying

    Currying是一種函數式編程技巧, 指的是把接受多個參數的函數變換成接受一個單一參數的函數. 以一個簡單的例子在Scala中實現.. def f(a:Int, b:Int)={ a+b } //f ...

  5. IDEA 中,编译后不拷贝 mybatis 配置的 mapper 的 xml 文件

    在maven项目的pom.xml配置文件里添加 <build> <resources> <resource> <directory>src/main/j ...

  6. BJFU 1549 ——Candy——————【想法题】

    Candy 时间限制(C/C++):1000MS/3000MS          运行内存限制:65536KByte总提交:40            测试通过:20 描述 There are N c ...

  7. bzoj 5291: [Bjoi2018]链上二次求和

    Description 有一条长度为n的链(1≤i<n,点i与点i+1之间有一条边的无向图),每个点有一个整数权值,第i个点的权值是 a_i.现在有m个操作,每个操作如下: 操作1(修改):给定 ...

  8. java版两人聊天程序

    server.java import java.io.*; import java.net.*; import java.text.SimpleDateFormat; import java.util ...

  9. Node.js之Buffer

    JavaScript 语言自身只有字符串数据类型,没有二进制数据类型.但在处理像TCP流或文件流时,必须使用到二进制数据.因此在 Node.js中,定义了一个 Buffer 类,该类用来创建一个专门存 ...

  10. C#中TransactionScope的使用方法和原理(摘)

    出自51CTO博客:http://cnn237111.blog.51cto.com/2359144/1271600 在.net 1.1的时代,还没有TransactionScope类,因此很多关于事务 ...