ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Attibutes(特性)
1.A,示例(Sample) 返回顶部

“特性”示例

本示例演示了如何创建自定义特性类,如何在代码中使用这些类,以及如何通过反射查询它们。有关特性的其他信息,请参见特性(C# 编程指南).

安全说明

提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任。

在 Visual Studio 中生成并运行“特性”示例

  • 在“调试”菜单上,单击“开始执行(不调试)”。

从命令行生成并运行“特性”示例

  • 在命令提示符处,键入以下命令:

    csc AttributesTutorial.cs
    AttributesTutorial
1.B,示例代码(Sample Code)返回顶部

1.B.1, AttributesTutorial.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。 // AttributesTutorial.cs
// 本示例表明类特性和方法特性的用法。 using System;
using System.Reflection;
using System.Collections; // IsTested 类是用户定义的自定义特性类。
// 它可以应用于任何声明,包括
// - 类型(结构、类、枚举、委托)
// - 成员(方法、字段、事、属性、索引器)
// 使用它时不带参数。
public class IsTestedAttribute : Attribute
{
public override string ToString()
{
return "Is Tested";
}
} // AuthorAttribute 类是用户定义的特性类。
// 它只能应用于类和结构声明。
// 它采用一个未命名的字符串参数(作者的姓名)。
// 它有一个可选的命名参数 Version,其类型为 int。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class AuthorAttribute : Attribute
{
// 此构造函数为特性类指定未命名的参数。
public AuthorAttribute(string name)
{
this.name = name;
this.version = ;
} // 此属性为只读(它没有 set 访问器)
// 因此,不能将其用作此特性的命名参数。
public string Name
{
get
{
return name;
}
} // 此属性可读写(它有 set 访问器)
// 因此,将此类用作属性类时,
// 可以将其用作命名参数。
public int Version
{
get
{
return version;
}
set
{
version = value;
}
} public override string ToString()
{
string value = "Author : " + Name;
if (version != )
{
value += " Version : " + Version.ToString();
}
return value;
} private string name;
private int version;
} // 此处,将用户定义的自定义特性 AuthorAttribute 附加
// 到 Account 类。创建属性时,会将未命名的
// 字符串参数传递到 AuthorAttribute 类的构造函数。
[Author("Joe Programmer")]
class Account
{
// 将自定义特性 IsTestedAttribute 附加到此方法。
[IsTested]
public void AddOrder(Order orderToAdd)
{
orders.Add(orderToAdd);
} private ArrayList orders = new ArrayList();
} // 将自定义特性 AuthorAttribute 和 IsTestedAttribute 附加
// 到此类。
// 请注意 AuthorAttribute 的命名参数“Version”的用法。
[Author("Jane Programmer", Version = ), IsTested()]
class Order
{
// 在此处添加资料...
} class MainClass
{
private static bool IsMemberTested(MemberInfo member)
{
foreach (object attribute in member.GetCustomAttributes(true))
{
if (attribute is IsTestedAttribute)
{
return true;
}
}
return false;
} private static void DumpAttributes(MemberInfo member)
{
Console.WriteLine("Attributes for : " + member.Name);
foreach (object attribute in member.GetCustomAttributes(true))
{
Console.WriteLine(attribute);
}
} public static void Main()
{
// 显示 Account 类的特性
DumpAttributes(typeof(Account)); // 显示已测试成员的列表
foreach (MethodInfo method in (typeof(Account)).GetMethods())
{
if (IsMemberTested(method))
{
Console.WriteLine("Member {0} is tested!", method.Name);
}
else
{
Console.WriteLine("Member {0} is NOT tested!", method.Name);
}
}
Console.WriteLine(); // 显示 Order 类的特性
DumpAttributes(typeof(Order)); // 显示 Order 类的方法的特性
foreach (MethodInfo method in (typeof(Order)).GetMethods())
{
if (IsMemberTested(method))
{
Console.WriteLine("Member {0} is tested!", method.Name);
}
else
{
Console.WriteLine("Member {0} is NOT tested!", method.Name);
}
}
Console.WriteLine();
}
}

1.B.2,

1.C,下载地址(Free Download)返回顶部
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

最新文章

  1. java web后台开发SSM框架(Spring+SpringMVC+MyBaitis)搭建与优化
  2. 【代码笔记】iOS-圆角矩形
  3. CLR简介(一)
  4. gitlab+gerrit+jenkins持续集成框架
  5. linux keepalived+LVS 实现mysql 从库负载均衡
  6. 禁止用户自己停止SEP - 飞舞的菜刀 - 51CTO技术博客
  7. 【转载】OLE DB, ADO, ODBC关系与区别
  8. C++中执行windows指令
  9. 自定义checkbox样式
  10. Let's Format Css Documents
  11. java 缓存ehcache的使用(使用方式一)
  12. vue实现图书管理demo
  13. npm学习总结
  14. npm -S -D -g i 有什么区别
  15. Centos 6.8编译安装LNMP环境
  16. HDU - 1540 Tunnel Warfare(线段树区间合并)
  17. java-Date类
  18. ubuntu 上 SSH scp 技巧
  19. MYSQL中替换oracle中runum用法
  20. sqlplus sys as sysdba

热门文章

  1. [bzoj3231][SDOI2008]递归数列——矩阵乘法
  2. ios 里如何处理四舍五入的问题
  3. python学记笔记 2 异步IO
  4. Oracle rman 全备份的一个小例子
  5. 【bzoj2796】 [Poi2012]Fibonacci Representation
  6. jQuery点击自身以外地方关闭弹出层
  7. mybatis generator 生成javabean自定义类型转换
  8. Solidity 文档--第二章:安装 Solidity
  9. TCP 粘包及其解决方案(zz)
  10. 从事前端开发必须要了解的CSS原理