一个是属性,用于存取类的字段,一个是特性,用来标识类,方法等的附加性质。

属性:

  1. class TimePeriod
  2. {
  3. private double seconds;
  4.  
  5. public double Hours
  6. {
  7. get { return seconds / ; }
  8. set { seconds = value * ; }
  9. }
  10. }
  11.  
  12. class Program
  13. {
  14. static void Main()
  15. {
  16. TimePeriod t = new TimePeriod();
  17.  
  18. // Assigning the Hours property causes the 'set' accessor to be called.
  19. t.Hours = ;
  20.  
  21. // Evaluating the Hours property causes the 'get' accessor to be called.
  22. System.Console.WriteLine("Time in hours: " + t.Hours);
  23. }
  24. }
  25. // Output: Time in hours: 24

特性:

  1. using System;
  2. using System.Reflection;
  3.  
  4. namespace CustomAttrCS
  5. {
  6. // An enumeration of animals. Start at 1 (0 = uninitialized).
  7. public enum Animal
  8. {
  9. // Pets.
  10. Dog = ,
  11. Cat,
  12. Bird,
  13. }
  14.  
  15. // A custom attribute to allow a target to have a pet.
  16. public class AnimalTypeAttribute : Attribute
  17. {
  18. // The constructor is called when the attribute is set.
  19. public AnimalTypeAttribute(Animal pet)
  20. {
  21. thePet = pet;
  22. }
  23.  
  24. // Keep a variable internally ...
  25. protected Animal thePet;
  26.  
  27. // .. and show a copy to the outside world.
  28. public Animal Pet
  29. {
  30. get { return thePet; }
  31. set { thePet = Pet; }
  32. }
  33. }
  34.  
  35. // A test class where each method has its own pet.
  36. class AnimalTypeTestClass
  37. {
  38. [AnimalType(Animal.Dog)]
  39. public void DogMethod() { }
  40.  
  41. [AnimalType(Animal.Cat)]
  42. public void CatMethod() { }
  43.  
  44. [AnimalType(Animal.Bird)]
  45. public void BirdMethod() { }
  46. }
  47.  
  48. class DemoClass
  49. {
  50. static void Main(string[] args)
  51. {
  52. AnimalTypeTestClass testClass = new AnimalTypeTestClass();
  53. Type type = testClass.GetType();
  54. // Iterate through all the methods of the class.
  55. foreach (MethodInfo mInfo in type.GetMethods())
  56. {
  57. // Iterate through all the Attributes for each method.
  58. foreach (Attribute attr in
  59. Attribute.GetCustomAttributes(mInfo))
  60. {
  61. // Check for the AnimalType attribute.
  62. if (attr.GetType() == typeof(AnimalTypeAttribute))
  63. Console.WriteLine(
  64. "Method {0} has a pet {1} attribute.",
  65. mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
  66. }
  67. }
  68. }
  69. }
  70. }
  71.  
  72. /*
  73. * Output:
  74. * Method DogMethod has a pet Dog attribute.
  75. * Method CatMethod has a pet Cat attribute.
  76. * Method BirdMethod has a pet Bird attribute.
  77. */

property 与 attribute 的区别?的更多相关文章

  1. DOM 中 Property 和 Attribute 的区别

    原文地址:http://web.jobbole.com/83129/ property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute: ...

  2. C#中Property和Attribute的区别

    C#中Property和Attribute的区别 Attribute 字段Property 属性(get;set;) 属性的正常写: private string name; public strin ...

  3. Property 和 Attribute 的区别(转)

    property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...

  4. [转]DOM 中 Property 和 Attribute 的区别

    angular的文档: https://angular.io/guide/template-syntax#property-binding https://blog.csdn.net/sunq1982 ...

  5. JavaScript 中 Property 和 Attribute 的区别详解

    property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...

  6. C#中 property 与 attribute的区别?

    C#中 property 与 attribute的区别?答:attribute:自定义属性的基类;property :类中的属性

  7. DOM 中 Property 和 Attribute 的区别(转)

    property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...

  8. property和attribute的区别

    property是指类向外提供的数据区域.而attribute则是描述对象在编译时或运行时属性的,分为固有型和用户自定义型,其中用户自定义型可以利用Reflection在运行期获取.这两者是有本质区别 ...

  9. Property和attribute的区别[转]

    Attribute和Property都可以翻译成“属性”,有的地方用Attribute表示“属性”,有的地方又在用Property,初 学者常常在这两个单词间“迷失”,甚至认为二者没有区别,是一样的. ...

  10. Property与Attribute的区别

    Property属于面向对象的范畴----属性 Attribute则是编程语言文法层面的东西----特征          Property属于面向对象的范畴.在使用面向对象编程的时候,常常需要对客观 ...

随机推荐

  1. usaco-2.2.2Subset Sums 集合

    01背包,对每个数至多取一次,为了避免重复,应倒序dp usaco-2.2.2Subset Sums 集合 时间限制: 1 Sec  内存限制: 128 MB 题目描述 对于从1到N的连续整集合合,能 ...

  2. Codeforces Beta Round #2 B. The least round way dp

    B. The least round way 题目连接: http://www.codeforces.com/contest/2/problem/B Description There is a sq ...

  3. 让Code First下的数据库的迁移更加简单

    Code First给我们的程序开发带了很多便利,之前的版本中一个比较不大方便的地方是数据库迁移,麻烦不说,往往还和上下文相关,在不同的版本之间的数据库进行迁移还很容易失败,并且一旦失败还不大容易找到 ...

  4. JS操作小数运算,结果莫名其妙出现多位小数问题

    Number类型: Number类型是ECMAScript中最常用和最令人关注的类型了:这种类型使用IEEE754格式来表示整数和浮点数值(浮点数值在某些语言中也被成为双精度数值),为支持各种数据类型 ...

  5. Oracle一条SQL语句时快时慢

    今天碰到一个非常奇怪的问题问题,一条SQL语句在PL/SQL developer中很慢,需要9s,问题SQL: SELECT * FROM GG_function_location f WHERE f ...

  6. 统一D3D与OpenGL坐标系统

    作者:游蓝海(http://blog.csdn.net/you_lan_hai) DirectX 3D与OpenGL坐标系统的差异性,给我们带来非常大的麻烦.让跨平台编程的新手非常困惑.近期在做一个跨 ...

  7. android4.1 JELLY_BEAN:All WebView methods must be called on the same thread[问题已解决]

    11-06 18:29:15.582: W/WebView(27807): java.lang.Throwable: A WebView method was called on thread 'Ja ...

  8. IP编址

    IP地址 /include/linux/inetdevice.h,定义IPV4专用的网络设备相关的结构.宏等 /net/ipv4/devinet.c.支持IPV4特性的设备操作接口 数据组织 net_ ...

  9. Wlms进程导致Windows2008R2操作系统关机的解决办法

    2.将wlms进程干掉,将PStooLs工具copy至服务器的C盘根目录下 3.运行psexec.exe -d -i -s regedit.exe 命令 4.打开注册表,找到vlms选项, [HKEY ...

  10. MySQL分库备份与分表备份

    MySQL分库备份与分表备份 1.分库备份 要求:将mysql数据库中的用户数据库备份,备份的数据库文件以时间命名 脚本内容如下: [root@db01 scripts]# vim backup_da ...