C#的Attribute
2015-09-04 09:03
621 查看
using System; using System.Collections; using System.Collections.Generic; using System.IO; namespace codeTest { class Program { static void Main(string[] args) { //通过反射来获取Attribute中的信息 MyAttribute myattribute; foreach (var attr in typeof(MyClass).GetCustomAttributes(true)) { myattribute = attr as MyAttribute; Console.WriteLine(myattribute.Name); } Console.ReadLine(); } } //自定义Attribute经常用到 AttributeUsage ,可以限制自定义Attribute的使用范围 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] class MyAttribute : Attribute { //MyAttribute必须继承Attribute string name; public MyAttribute(string nameIn) { this.name = nameIn; } public string Name { get { return this.name; } } public string desc { get; set; } } //MyAttribute可以缩写为 My [My("MyAttribute",desc="MyClass")] class MyClass { } }
相关文章推荐
- C#通过WebBrowser快速扒站思路积累大量着陆页列表
- C# 比较目录
- Clr Via C#读书笔记---线程基础
- C# 股票 蜡烛图 显示
- c# 关于一些数值转换的整理(部分)
- c#中的delegate(委托)和event(事件)
- C#动态编译引擎-CS-Script 简单使用
- C#动态编译引擎-CS-Script 简单使用
- C#判断系统是64位还是32位 支持.net4.0以前的版本
- C# 过滤特殊字符
- C# Excel 生成图表,添加趋势线、误差线
- C# Excel 为图表添加趋势线、误差线
- C#编码标准和命名规范
- C#常用控件缩写
- C#编程总结(一)序列化
- C#编程总结(二)多线程基础
- C#编程总结(三)线程同步
- C#编程总结(四)多线程应用
- C#编程总结(五)多线程带给我们的一些思考
- C#编程总结(六)异步编程