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, ...
随机推荐
- android usb Host模式下与usb Hid 设备的通信
做android 与USB HID设备的通信有段时间了,总结一下遇到的问题和解决方法: 1,第一次遇到的问题:android 版本低不支持usb hid, 被要求做相关项目的时候,就从mUsbMana ...
- 从 C++ 到 Qt(命令行编译)good
从 C++ 到 Qt 转载自:http://hi.baidu.com/cyclone/blog/item/8f8f08fa52d22f8758ee9006.html Qt 是 C++ 的库,Qt在an ...
- mysql 有索引没走索引 更新锁全表
Session 1: mysql> select connection_id(); +-----------------+ | connection_id() | +-------------- ...
- 结构体dict_field_t
typedef struct dict_field_struct dict_field_t; typedef struct dict_field_struct dict_field_t; /** Da ...
- 函数lock_mode_stronger_or_eq 锁权限等级
row代表lock HashTable的权限 column代表预加锁的权限 ulint lock_mode_stronger_or_eq( /*=====================*/ e ...
- jquery图片播放插件Fancybox(灯箱)
效果预览Demo源码下载 Fancybox的特点如下: 可以支持图片.html文本.flash动画.iframe以及ajax的支持 可以自定义播放器的CSS样式 可以以组的形式进行播放 如果将鼠标滚动 ...
- Spring概述--1
1.1.1 Spring是什么 Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发.应用程序是由 ...
- [NOIP2011]数的划分
本题地址:http://www.luogu.org/problem/show?pid=1025 题目描述 将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序).例如:n=7,k=3,下面三 ...
- LCD驱动(FrameBuffer)实例开发讲解
一.开发环境 主 机:VMWare--Fedora 9 开发板:Mini2440--64MB Nand, Kernel:2.6.30.4 编译器:arm-linux-gcc-4.3.2 二.背景知识 ...
- test-from
title header1 hahhahjl header2 adfkljasd $$a^2$$ point 1 point 2 | table1 | table2 | | ---- | ---- | ...