property 与 attribute 的区别?
一个是属性,用于存取类的字段,一个是特性,用来标识类,方法等的附加性质。
属性:
- class TimePeriod
- {
- private double seconds;
- public double Hours
- {
- get { return seconds / ; }
- set { seconds = value * ; }
- }
- }
- class Program
- {
- static void Main()
- {
- TimePeriod t = new TimePeriod();
- // Assigning the Hours property causes the 'set' accessor to be called.
- t.Hours = ;
- // Evaluating the Hours property causes the 'get' accessor to be called.
- System.Console.WriteLine("Time in hours: " + t.Hours);
- }
- }
- // Output: Time in hours: 24
特性:
- using System;
- using System.Reflection;
- namespace CustomAttrCS
- {
- // An enumeration of animals. Start at 1 (0 = uninitialized).
- public enum Animal
- {
- // Pets.
- Dog = ,
- Cat,
- Bird,
- }
- // A custom attribute to allow a target to have a pet.
- public class AnimalTypeAttribute : Attribute
- {
- // The constructor is called when the attribute is set.
- public AnimalTypeAttribute(Animal pet)
- {
- thePet = pet;
- }
- // Keep a variable internally ...
- protected Animal thePet;
- // .. and show a copy to the outside world.
- public Animal Pet
- {
- get { return thePet; }
- set { thePet = Pet; }
- }
- }
- // A test class where each method has its own pet.
- class AnimalTypeTestClass
- {
- [AnimalType(Animal.Dog)]
- public void DogMethod() { }
- [AnimalType(Animal.Cat)]
- public void CatMethod() { }
- [AnimalType(Animal.Bird)]
- public void BirdMethod() { }
- }
- class DemoClass
- {
- static void Main(string[] args)
- {
- AnimalTypeTestClass testClass = new AnimalTypeTestClass();
- Type type = testClass.GetType();
- // Iterate through all the methods of the class.
- foreach (MethodInfo mInfo in type.GetMethods())
- {
- // Iterate through all the Attributes for each method.
- foreach (Attribute attr in
- Attribute.GetCustomAttributes(mInfo))
- {
- // Check for the AnimalType attribute.
- if (attr.GetType() == typeof(AnimalTypeAttribute))
- Console.WriteLine(
- "Method {0} has a pet {1} attribute.",
- mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
- }
- }
- }
- }
- }
- /*
- * Output:
- * Method DogMethod has a pet Dog attribute.
- * Method CatMethod has a pet Cat attribute.
- * Method BirdMethod has a pet Bird attribute.
- */
property 与 attribute 的区别?的更多相关文章
- DOM 中 Property 和 Attribute 的区别
原文地址:http://web.jobbole.com/83129/ property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute: ...
- C#中Property和Attribute的区别
C#中Property和Attribute的区别 Attribute 字段Property 属性(get;set;) 属性的正常写: private string name; public strin ...
- Property 和 Attribute 的区别(转)
property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...
- [转]DOM 中 Property 和 Attribute 的区别
angular的文档: https://angular.io/guide/template-syntax#property-binding https://blog.csdn.net/sunq1982 ...
- JavaScript 中 Property 和 Attribute 的区别详解
property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...
- C#中 property 与 attribute的区别?
C#中 property 与 attribute的区别?答:attribute:自定义属性的基类;property :类中的属性
- DOM 中 Property 和 Attribute 的区别(转)
property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...
- property和attribute的区别
property是指类向外提供的数据区域.而attribute则是描述对象在编译时或运行时属性的,分为固有型和用户自定义型,其中用户自定义型可以利用Reflection在运行期获取.这两者是有本质区别 ...
- Property和attribute的区别[转]
Attribute和Property都可以翻译成“属性”,有的地方用Attribute表示“属性”,有的地方又在用Property,初 学者常常在这两个单词间“迷失”,甚至认为二者没有区别,是一样的. ...
- Property与Attribute的区别
Property属于面向对象的范畴----属性 Attribute则是编程语言文法层面的东西----特征 Property属于面向对象的范畴.在使用面向对象编程的时候,常常需要对客观 ...
随机推荐
- usaco-2.2.2Subset Sums 集合
01背包,对每个数至多取一次,为了避免重复,应倒序dp usaco-2.2.2Subset Sums 集合 时间限制: 1 Sec 内存限制: 128 MB 题目描述 对于从1到N的连续整集合合,能 ...
- 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 ...
- 让Code First下的数据库的迁移更加简单
Code First给我们的程序开发带了很多便利,之前的版本中一个比较不大方便的地方是数据库迁移,麻烦不说,往往还和上下文相关,在不同的版本之间的数据库进行迁移还很容易失败,并且一旦失败还不大容易找到 ...
- JS操作小数运算,结果莫名其妙出现多位小数问题
Number类型: Number类型是ECMAScript中最常用和最令人关注的类型了:这种类型使用IEEE754格式来表示整数和浮点数值(浮点数值在某些语言中也被成为双精度数值),为支持各种数据类型 ...
- Oracle一条SQL语句时快时慢
今天碰到一个非常奇怪的问题问题,一条SQL语句在PL/SQL developer中很慢,需要9s,问题SQL: SELECT * FROM GG_function_location f WHERE f ...
- 统一D3D与OpenGL坐标系统
作者:游蓝海(http://blog.csdn.net/you_lan_hai) DirectX 3D与OpenGL坐标系统的差异性,给我们带来非常大的麻烦.让跨平台编程的新手非常困惑.近期在做一个跨 ...
- 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 ...
- IP编址
IP地址 /include/linux/inetdevice.h,定义IPV4专用的网络设备相关的结构.宏等 /net/ipv4/devinet.c.支持IPV4特性的设备操作接口 数据组织 net_ ...
- Wlms进程导致Windows2008R2操作系统关机的解决办法
2.将wlms进程干掉,将PStooLs工具copy至服务器的C盘根目录下 3.运行psexec.exe -d -i -s regedit.exe 命令 4.打开注册表,找到vlms选项, [HKEY ...
- MySQL分库备份与分表备份
MySQL分库备份与分表备份 1.分库备份 要求:将mysql数据库中的用户数据库备份,备份的数据库文件以时间命名 脚本内容如下: [root@db01 scripts]# vim backup_da ...