惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
罗磊的独立博客
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
A
About on SuperTechFans
腾讯CDC
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
IT之家
IT之家
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
T
Tailwind CSS Blog
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog

博客园 - lhx

C# 非活动窗口截屏 用32位应用程序读取64位注册表中的Office key WMI 查找所有物理网卡 [转:IE编程] 如何设置IE8的WebBrowser控件(MSHTML) 的渲染模式 C#实现根据图片的EXIF自动调整图片方向&lt;转&gt; memory released when you minimize the app 自定义控件属性的特性大全&lt;转&gt; 详解自定义托管宿主WCF解决方案开发配置过程(1)【转】 对比两个同类型的List<T>返回差异List<T>集合 - lhx - 博客园 如何给DataGridViewComboBoxColumn写事件 将Excel的数据导入DataGridView中[原创] 将DataGridView选中行的值填充到符合命名规则的控件中[原创] Oracle 存储过程 例子~! oracle 存储过程的基本语法(转) 在linux下配置Struts中的Oracle 数据源(原创) 如何给linux添加新硬盘(转) linux 下 mysql-5.0.22. 的安装(转) jQuery Ajax 全解析 (转) tomcat6_jdk1.6_安装配置_开启自动运行,普通用户执行 (转)
.NET 特性Attribute
lhx · 2009-01-02 · via 博客园 - lhx

Attribute类

除了.NET内置提供的一些特性外,我们当然也可以自定义自己的Attribute。需要知道的中,所有自定义的Attribute都必须派生自类Attribute。在开发自定义的Attribute的时候,还有一点需要注意的是特性可以施加到不同的元素中,如方法,属性,类,参数等。有时候我们可能希望自定义的特性只允许施加到类中,这时还可以使用AttributeUsage限定特性的使用范围。

以下给出一个使用示例

using System; 
namespace AttTargsCS 
// 该Attribute只对类有效. 
   [AttributeUsage(AttributeTargets.Class)]
   
public class ClassTargetAttribute : Attribute 
   { 
    } 
// 该Attribute只对方法有效. 
   [AttributeUsage(AttributeTargets.Method)]
   
public class MethodTargetAttribute : Attribute 
   { 
    } 
// 该Attribute只对构造器有效。
   [AttributeUsage(AttributeTargets.Constructor)]
   
public class ConstructorTargetAttribute : Attribute 
   { 
    } 
// 该Attribute只对字段有效. 
   [AttributeUsage(AttributeTargets.Field)]
   
public class FieldTargetAttribute : Attribute
   {
   } 
// 该Attribute对类或者方法有效(组合). 
  [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
   
public class ClassMethodTargetAttribute : Attribute
   {
    } 
// 该Attribute对所有的元素有效.
   [AttributeUsage(AttributeTargets.All)]
   
public class AllTargetsAttribute : Attribute 
  { 
   } 
//上面定义的Attribute施加到程序元素上的用法
   [ClassTarget]  //施加到类
   [ClassMethodTarget]//施加到类
   [AllTargets] //施加到类
   public class TestClassAttribute
   { 
      [ConstructorTarget] 
//施加到构造器
      [AllTargets] //施加到构造器
      TestClassAttribute()
      { 
       } 

      [MethodTarget] 

//施加到方法
      [ClassMethodTarget] //施加到方法
      [AllTargets] //施加到方法
      public void Method1()
      {
      }
     
      [FieldTarget] 
//施加到字段
      [AllTargets] //施加到字段
      public int myInt; static void Main(string[] args)
      { 
      } 
   }