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类的具体实现
}
转载于:https://www.cnblogs.com/lvdongjie/p/5000383.html
C# Attribute(中)——Attribute本质论相关推荐
- C#中Attribute的继承
在C#中Attribute是个非常有用的语法,本文不会介绍Attribute的使用方法,如果想了解Attribute的详细信息请查阅MSDN及网上相关文档.C#中的Attribute有两个地方是和继承 ...
- C#中Attribute的应用
Attribute的基本概念 经常有朋友问,Attribute是什么?它有什么用?好像没有这个东东程序也能运行.实际上在.Net中,Attribute是一个非常重要的组成部分,为了帮助大家理解和掌握A ...
- Unity c#中Attribute用法详解
举两个例子,在变量上使用[SerializeFiled]属性,可以强制让变量进行序列化,可以在Unity的Editor上进行赋值. 在Class上使用[RequireComponent]属性,就会在C ...
- SAP WebClient UI开发工具中attribute文件夹展开的实现原理分析
For project reason I need to figure out the logic how the field list is assembled when folder " ...
- java中attribute用法_Java FieldPosition getFieldAttribute()用法及代码示例
java.text.FieldPosition类的getFieldAttribute()方法用于获取Format.field形式的字段标识符. 用法: public Format.Field getF ...
- android xml中attribute,android – 我无法从我的XML资源中读取AttributeSet
问题是对解析器功能的误解.行后: XmlResourceParser parser = r.getXml(R.layout.testcameraoverlay); 解析器位于文档的开头,并且尚未读取任 ...
- 浅谈Attribute [C# | Attribute | DefaultValueAttribute]
正文: 先看我的错误代码: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Field | AttributeTar ...
- “.NET研究”关于C# 中的Attribute 特性
Attribute与Proper上海企业网站制作ty 的翻译区别 Attribute 一般译作"特性",Property 仍然译为"属性". Attribute ...
- 深入浅出Attribute (中)——Attribute本质论
深入浅出Attribute(中)--Attribute本质论 CSDN 刘铁猛 2007-06-04 小序: 上篇里,我们把Attribute"粘"在类的成员方法上show了一把, ...
最新文章
- html中运行php脚本,php脚本在html文件中
- 刚刚!最新大学专业排名出炉:北大、清华和浙大居前三!
- Tensorflow学习: 乘法demo
- 网站api自己怎么写_网站描述怎么写?对网站优化有什么作用?
- mac恢复iphone_免费下载:旧Mac和iPhone壁纸的令人震惊的完整档案
- 关于DataFormWebPart中CreatedModifiedInfo信息的分开使用
- UI设计的扁平化图标模板素材
- 坚果nuts 加速 官网_坚果?R2?正式发布:延续经典设计,提供旗舰性能,而且是“下一代电脑”...
- 2.8 计算图的导数计算
- 带项目的学问,如何带半路项目
- mysql数据库随机生成数据库_MySQL 从零开始:08 番外:随机生成数据库数据
- 青海贵德黄河岸边现雾凇奇观
- 做跨境电商,先要有个靠谱的商城系统
- php \n不能换行
- 控制Dell PowerEdge R720的风扇转速 (利用IPMI)
- 舆情监控系统python开源_舆情监测系统开源
- Java运行时常量池
- 餐饮业实施5S管理的效益及实施方案
- 北京明年拟新增3万个幼儿园学位 并再筹建6万套政策性产权房
- 网页yy语音(歪歪语音) 网页版
热门文章
- 很喜欢VS.NET 2003对条件编译的代码提示
- BZOJ1016 [JSOI2008]最小生成树计数
- angular2 学习笔记 ( unit test 单元测试 )
- listView基本实现
- 发行版迁移及二进制兼容性注意事项
- linux 架设J2EE网站过程分享之二 —— JDK安装
- Vue中组件间通信的方式
- json.stringfy()和json.parse()
- openresty安装(centos)
- devsecops_DevSecOps管道和工具:您需要了解的内容