C# Attribute(中)——Attribute本质论
using System;
using System.Diagnostics;
namespace Sample
{
class Program
{
[Conditional("OK")]
public static void TargetMethod()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\t=<水之真谛>=\nhttp://blog.csdn.net/FantasiaX\n\n");
}
static void Main(string[] args)
{
TargetMethod();
}
}
}
// 上善若水,润物无声 //
/* [url]http://blog.csdn.net/FantasiaX[/url] */
using System;
namespace OysterAttributeSample
{
class Oyster: System.Attribute // 必需以System.Attribute类为基类
{
// Kind属性,默认值为null
private string kind;
public string Kind
{
get { return kind; }
set { kind = value; }
}
// Age属性,默认值为
private uint age;
public uint Age
{
get { return age; }
set { age = value; }
}
// 值为null的string是危险的,所以必需在构造函数中赋值
public Oyster(string arg) // 定位参数
{
this.Kind = arg;
}
}
[Oyster("Thorny ", Age=3)] // 3年的多刺牡蛎附着在轮船(这是一个类)上。注意:对属性的赋值是在圆括号里完成的!
class Ship
{
[Oyster("Saddle")] // 0年的鞍形牡蛎附着在船舵(这是一个数据成员)上,Age使用的是默认值,构造函数的参数必需完整
public string Rudder;
}
class Program
{
static void Main(string[] args)
{
// ... 使用反射来读取Attribute
}
}
}
// [url]http://blog.csdn.net/FantasiaX[/url]
using System;
namespace AttributeTargetValue
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Assembly\t\t\t{0}", Convert.ToInt32(AttributeTargets.Assembly));
Console.WriteLine("Module\t\t\t\t{0}", Convert.ToInt32(AttributeTargets.Module));
Console.WriteLine("Class\t\t\t\t{0}", Convert.ToInt32(AttributeTargets.Class));
Console.WriteLine("Struct\t\t\t\t{0}", Convert.ToInt32(AttributeTargets.Struct));
Console.WriteLine("Enum\t\t\t\t{0}", Convert.ToInt32(AttributeTargets.Enum));
Console.WriteLine("Constructor\t\t\t{0}", Convert.ToInt32(AttributeTargets.Constructor));
Console.WriteLine("Method\t\t\t\t{0}", Convert.ToInt32(AttributeTargets.Method));
Console.WriteLine("Property\t\t\t{0}", Convert.ToInt32(AttributeTargets.Property));
Console.WriteLine("Field\t\t\t\t{0}", Convert.ToInt32(AttributeTargets.Field));
Console.WriteLine("Event\t\t\t\t{0}", Convert.ToInt32(AttributeTargets.Event));
Console.WriteLine("Interface\t\t\t{0}", Convert.ToInt32(AttributeTargets.Interface));
Console.WriteLine("Parameter\t\t\t{0}", Convert.ToInt32(AttributeTargets.Parameter));
Console.WriteLine("Delegate\t\t\t{0}", Convert.ToInt32(AttributeTargets.Delegate));
Console.WriteLine("ReturnValue\t\t\t{0}", Convert.ToInt32(AttributeTargets.ReturnValue));
Console.WriteLine("GenericParameter\t\t{0}", Convert.ToInt32(AttributeTargets.GenericParameter));
Console.WriteLine("All\t\t\t\t{0}", Convert.ToInt32(AttributeTargets.All));
Console.WriteLine("\n");
}
}
}
class Oyster : System.Attribute
{
// OysterAttribute类的具体实现
}
class Oyster : System.Attribute
{
// OysterAttribute类的具体实现
}
C# Attribute(中)——Attribute本质论的更多相关文章
- C#中Attribute的继承
在C#中Attribute是个非常有用的语法,本文不会介绍Attribute的使用方法,如果想了解Attribute的详细信息请查阅MSDN及网上相关文档.C#中的Attribute有两个地方是和继承 ...
- javascript中attribute和property的区别详解
DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...
- Net中Attribute特性的高级使用及自定义验证实现
好久没写博客了,今天在百忙之中抽空来写篇文章,记录一下最近深入学习Attribute特性的笔记及心得.~~ 一.什么是特性? 特性(Attribute)是用于在运行时传递程序中各种元素(比如类.方法. ...
- C#中Attribute介绍
什么是特性? MSDN中定义为:公共语言运行时运行添加类似关键字的描述声明,叫做Attribute,它对程序中的元素进行标注,如类型.方法.字段和属性等.attribute和Microsoft.Net ...
- Request中Attribute 和 Parameter 的区别
Attribute 和 Parameter 的区别 (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为 ...
- js中Attribute和property的区别与联系
相信大多数的初学者对js中的property和attribute的关系很容易搞混, Attribute大多用于DOM的操作中,比如ele.attributes指的是一个元素的特性集合,是一个nodel ...
- C#中Attribute和Property
XAML是XML派生而来的语言,所以很多XML中的概念在XAML中是通用的. 为了表示同类标签中的某个标签与众不同,可以给它的特征(Attribute)赋值,为特征值赋值的语法如下: 非空标签:< ...
- C#中Attribute/特性的使用
类似Java的注解/Annotation 特性是用于在运行时传递程序中各种元素(比如类.方法.结构.枚举.组件等)的行为信息的声明性标签,这个标签可以有多个.您可以通过使用特性向程序添加声明性信息.一 ...
- css中attribute selector及pseudo class
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference#Selectors 在css3规范中,定义了以下几种类型的selector: Ba ...
- JS中attribute和property的区别
attribute是HTML标签上的特性,它的值只能够是字符串:html 上id,class property是JavaScript里定义的对象: 如var obj={x:1,y:2} ,这里x, ...
随机推荐
- 乐1/MACBOOK/ N1 Type-C接口新体验
经过在华强北电子市场排队一个小时,笔者顺利买到了期待已久的乐1,结合之前的NOKIA平板电脑N1,苹果全新MACBOOK,终于集齐了手机.平板.笔记本电脑三种TYPE-C接口设备(能兑换极品装备吗?^ ...
- background image position问题
在CSS中,背景图片的定位方法有3种: 1)关键字:background-position: top left; 2)像素:background-position: 0px 0px; 3)百分比:ba ...
- 【HDOJ】1706 The diameter of graph
这么个简单的题目居然没有人题解.floyd中计算数目,同时注意重边. /* 1706 */ #include <iostream> #include <string> #inc ...
- c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出
Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...
- Axis2联接WCF(比较完整的版本)
Axis2联接WCF(比较完整的版本) 分basicHttpBinding和wsHttpBinding两种情况: 一.basicHttpBinding比较简单一点,先来看看它所要求的HTTP包:POS ...
- JAX-RS -- Java API for RESTful Web Services
Java EE 6 引入了对 JSR-311 的支持.JSR-311(JAX-RS:Java API for RESTful Web Services)旨在定义一个统一的规范,使得 Java 程序员可 ...
- spring-- 事务--9
9.1 数据库事务概述 事务首先是一系列操作组成的工作单元,该工作单元内的操作是不可分割的,即要么所有操作都做,要么所有操作都不做,这就是事务. 事务必需满足ACID(原子性.一致性.隔离性和持久性 ...
- [liu yanling]测试用例设计综合策略
Myers提出了使用各种测试方法的综合策略: 1) 在任何情况下都必须使用边界值分析方法,经验表明用这种方法设计出测试用例发现程序错误的能力最强. 2) 必要时用等价类划分方法补充一些测试 ...
- eclipse tomcat内存溢出,加大内存
保存图片失败,请点击这里获得详细信息. 加入 -Xms256M -Xmx512M -XX:PermSize=256m -XX:MaxPermSize=512m
- Clean Code–Chapter 7 Error Handling
Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...