attribute的用法--C#

article/2025/10/7 1:52:34

一直以来都没理解attribute是个什么东西,也没怎么用,但是看msdn或者git上源码使用的还是蛮频繁的,今天好好整理了下,写下自己的理解和例子:

attribute主要用来说明代码段的的信息,标志等;可以一种元数据结构,不会影响到代码段的结果。这个代码段可以是class,struct,method,constructor等结构,下面会给出反编译源码说明哪些代码段可以作为目标。

    1,.NET内建attribute

         [AttributeUsage]

  AttributeUsage主要用来限定attribute可以在哪些情况下下使用,下面是AtttributeUsage的多个构造函数中的一个,其他不赘述:

internal AttributeUsageAttribute(AttributeTargets validOn, bool allowMultiple, bool inherited){this.m_attributeTarget = validOn;this.m_allowMultiple = allowMultiple;this.m_inherited = inherited;}

   参数说明:

       1),AttributeTarges必要的参数,反编译得到attribute的目标:    

public enum AttributeTargets{[__DynamicallyInvokable] Assembly = 1,[__DynamicallyInvokable] Module = 2,[__DynamicallyInvokable] Class = 4,[__DynamicallyInvokable] Struct = 8,[__DynamicallyInvokable] Enum = 16, // 0x00000010[__DynamicallyInvokable] Constructor = 32, // 0x00000020[__DynamicallyInvokable] Method = 64, // 0x00000040[__DynamicallyInvokable] Property = 128, // 0x00000080[__DynamicallyInvokable] Field = 256, // 0x00000100[__DynamicallyInvokable] Event = 512, // 0x00000200[__DynamicallyInvokable] Interface = 1024, // 0x00000400[__DynamicallyInvokable] Parameter = 2048, // 0x00000800[__DynamicallyInvokable] Delegate = 4096, // 0x00001000[__DynamicallyInvokable] ReturnValue = 8192, // 0x00002000[__DynamicallyInvokable] GenericParameter = 16384, // 0x00004000[__DynamicallyInvokable] All = GenericParameter | ReturnValue | Delegate | Parameter | Interface | Event | Field | Property | Method | Constructor | Enum | Struct | Class | Module | Assembly, // 0x00007FFF}

         2),allowMutiple是bool类型,可选的参数;ture表示可以在同一个代码段多次使用,默认的是false;

         3),inherited是bool类型,可选的参数;ture表示在派生类中继承,默认的值false;

         [Obsolete]

        主要用来指示代码段是废弃的,并通知编译器,编译器将会给出警告或者错误;

        用法:[Obsolete(message)]  和[Obsolte(message(string),iserror(bool))]

        message:描述代码段废弃的原因,并指出替代者;iserror:当它是true时,编译器报错,默认时false

    这里放代码的话看不出来编译错误,上图明显显示错误,并指示应该时NewMethod。

         [Conditional]

         主要用来定义一个预定义符号,作为编译条件,类似#ifdef的作用,下面例子说明用法:

#define Test
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace Experiments
{class Program{static void Main(string[] args){System.Console.ReadKey();DoWork();}[Conditional("Test")]static void DoWork(){for (int i = 0; i < 100; i++){Console.WriteLine(i);Thread.Sleep(100);}}}
}

  当没有定义#define Test,DoWork方法不执行

         [CallerMemberName]

  可以自动展示调用者的名字,用在INotifyPerprotyChanged例子:

public class MyUIClass : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;public void RaisePropertyChanged([CallerMemberName] string propertyName = null){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}private string _name;public string Name{get { return _name; }set{if (value != _name){_name = value;RaisePropertyChanged();   // notice that "Name" is not needed here explicitly}}}}

2,自定义attribute

     自定义的attribute必须要继承自Attribute基类,其参数按照MSDN解释分为位置参数(positional parameter)和可选的命名参数(named parameter)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Experiments
{[AttributeUsage(AttributeTargets.Class|AttributeTargets.Constructor|AttributeTargets.Field|AttributeTargets.Method|AttributeTargets.Property, AllowMultiple = true)]public class DevelopLog:Attribute{//positional parameterprivate string _developer;private string _reviewer;private string _lastModTime;//named parameterprivate string msg;         public string Developer { get => _developer;  }public string Reviewer { get => _reviewer; }public string LastModTime { get => _lastModTime;  }public string Msg { get => msg; set => msg = value; }public DevelopLog(string dev, string rv, string lmt){_developer = dev;_reviewer = rv;_lastModTime = lmt;}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Experiments
{[DevelopLog("zhangsan", "boss", "20180807", Msg = "create class")][DevelopLog("lisi", "boss", "20180807", Msg = "add method dowork")]public class Student{private string _name;private string _age;public Student(string n, string a){_name = n;_age = a;}[DevelopLog("zhangsan", "boss", "20180807")]public void EvertyDayDoThing(){}[DevelopLog("zhangsan", "boss", "20180807")]public void MoringDo(){}[DevelopLog("lisi", "boss", "20180808")]public void NoonDo(){}[DevelopLog("zhangsan", "boss", "20180807", Msg="paly game all day and not do homework")]public void PlayGame(){}}
}

  然后在实际应用中,我们可以通过reflection来获取上面描述的attribute,从而获取有价值的信息。


http://chatgpt.dhexx.cn/article/Jj9nxlw6.shtml

相关文章

attribute属性

attribute属性 __attribute__介绍 attribute的格式. __attribute__((attribute-list))attribute属性可以在编译的时候告诉编译器函数, 结构体的属性是什么, 进行某些编译优化, 也可以提供更加准确的错误检查. attribute是GNU特有的特性 这里主要说明attribute的4个属性, 分…

【C#】Attribute

原文链接&#xff1a;http://bbs.51aspx.com/showtopic-33963.html 前言 作为一个.NET开发人员&#xff0c;了解Attribute的重要性&#xff0c;用.NET大师Jeffrey Richter的话就是“任何.NET Framework 开发人员都有必要对定制attribute有一个牢靠的掌握”&#xff0c;所以掌…

attributes() 函数

查看更多 https://www.yuque.com/docs/share/a6cc2c96-9824-4903-acb8-284f4ebeb4fb

__attribute__ 用法

转自&#xff1a;http://www.cnblogs.com/astwish/p/3460618.html GNU C 的一大特色就是__attribute__ 机制。__attribute__ 可以设置函数属性&#xff08;Function Attribute &#xff09;、变量属性&#xff08;Variable Attribute &#xff09;和类型属性&#xff08;Type …

View绘制体系(三)——AttributeSet与TypedArray详解

View绘制体系&#xff08;三&#xff09;——AttributeSet与TypedArray详解 前言 上篇博客中讲了LayoutInflater.inflate机制&#xff0c;其中提到了AttributeSet和XmlPullParser两个接口&#xff0c;这里我们来详细的了解一下Android中提供的AttributeSet接口和它与XmlPullPa…

Android自定义View(三)自定义属性AttributeSet

自定义View的时候通常需要提供一些自定义属性&#xff0c;自定义属性非常简单&#xff0c;只需要在res资源目录的values目录下创建一个attrs.xml的属性定义文件&#xff0c;然后在该文件中定义相应的属性&#xff0c;并在自定义View的构造函数中获取并设置自定义属性的默认值即…

web搜索框的制作(必应)

前两天没事突然对搜索来了兴趣&#xff0c;我一直在想搜索框中我们输入一些字或者字母&#xff0c;为何下面就会有一些自动补齐的相关搜索&#xff0c;比如我在搜索输入框中输入一个字母e&#xff0c;下面就会出现饿了么&#xff0c;e租宝&#xff0c;ems等相关的搜索链接。然后…

html怎么调搜索框宽高,百度站内搜索css:输入框宽度及样式自定义

近日网站使用了百度站内搜索api&#xff0c;目的是为了提高站内搜索的速度&#xff0c;减轻查询站内数据库带来的服务器压力。 不过在使用百度站内搜索api(生效范围&#xff1a;*webkaka.com/*)后发现一个问题&#xff0c;不同的频道模版造成排版不合适的后果&#xff0c;如搜索…

织梦手机站站内搜索

今天在做手机网站发现一个问题&#xff0c;当在手机是使用搜索功能时马上就跳转到电脑端网站去了&#xff0c;在手机上无法使用。在网上找半天没有找到解决的办法&#xff0c;后来自己想通了&#xff0c;下面告诉大家怎么样简单的实现这个功能&#xff01;我的手机站是在m/这个…

基于 Elasticsearch 的站内搜索引擎实战

站内搜索&#xff0c;可以认为是针对一个网站特性内容的搜索功能。由于内容、格式可控&#xff0c;站内搜索比全网搜索的实现要简单很多。 简书这个网站本身自带一个搜索&#xff0c;但是缺乏针对个人文章的搜索&#xff0c;所以本文的实战内容是解决这个痛点。 代码在 https…

使用swiftype实现站内搜索

本人博客opiece.me&#xff0c;欢迎访问。 前言 首先&#xff0c;以下的内容是基于最新的swifytpe的教程&#xff0c;应该是2.0.0。 站内搜索顾名思义就是将范围限定在你的网站内&#xff0c;以此范围进行关键字搜索。 常见的站内搜索是google和baidu的&#xff0c;但是现在…

Compass实战 站内搜索

今天早上打算对这两天学习的Lucene以及Compass总结一下,想来想去,还是写个小项目来验证最好了。于是就有了今天的这篇文章。难易程度适合对于Compass或者Lucene刚入门的童鞋,大牛看到后望轻喷 :-) 项目预览项目需求项目目录核心处理 发帖部分查询部分总结项目预览 项目需求 …

html中的搜索

目录 hello&#x1f604; form表单&#x1f349; form的语法&#x1f34a; from的属性&#x1f34a; 提交&#xff1f;重置&#xff1f;&#x1f34a; 表单按钮&#xff08;html&#xff09;&#x1f50d; JavaScript提交表单&#x1f50d; JavaScript重置表单&#x1f…

必应(Bing)的站内搜索 site:<域名> <搜索内容>

最近在备考OCP&#xff0c;发现有一个网站的题库很好&#xff0c;就是www.examtopics.com&#xff0c;有很多Oracle的考题&#xff0c;都是在这里面搜到的&#xff0c;而且每道题都有人讨论。 为了加快搜索速度&#xff0c;提高精度&#xff0c;可以用Bing在这个网站内搜索&am…

百度站内搜索使用教程

最近做了一个博客CMS网站&#xff0c;用到了百度站内搜索&#xff0c;做一些必要的笔记&#xff0c;一来是对自己学习的知识的巩固&#xff0c;二来对有同样问题的人有参考作用 文章目录 一 使自己的网站被百度收录二 获取百度站内搜索代码三 总结 声明一下&#xff0c;我本人很…

利用免费的必应 Bing 自定义搜索打造站内全文搜索

简介 百度的站内搜索不做了&#xff0c;唉&#xff0c;果然免费的不永久。我们看看 Bing 的&#xff0c;每个月有 1000 次免费的调用 bing search api 的次数。不同客户可以多申请几个就行了。 申请入口&#xff1a; https://www.customsearch.ai&#xff0c;官方简介页面官方…

html百度站内搜索代码,网站添加百度站内搜索的教程

zblog博客程序中可以在侧边栏中添加搜索功能&#xff0c;但是让人郁闷的是如果没针对搜索使用搜索插件&#xff0c;那情况简直让人抓狂&#xff0c;还好我们可以使用百度的站内搜索功能&#xff0c;一方面可以节省网站的资源&#xff0c;另一方面可以增加百度的收率几率。 关于…

站内搜索

使用“site:”或者“domain:”来实现站内搜索 如果你想在一个特定的网站上来进行搜索&#xff0c;在众多庞大的信息流中找到你想要的信息&#xff0c; 在上篇中(http://blog.csdn.net/liunian02050328/article/details/8220379)介绍在java编程的环境下实现站内搜索&#xff0c;…

计算机网络中的ping什么意思,PING命令是什么?PING使用方法和参数详解

PING命令是用来检查本机于网络上的电脑是否正常通信的一个命令&#xff0c;作为一个网站的管理员、单位的网管这也是一个必会的命令。 因为网络中所有的电脑都有一个单独不会重复的IP地址&#xff0c;我们使用PING命令给目标IP地址发送一个数据包&#xff0c;对方就要返回一个同…

常见的ping命令

1.ping 延时和丢包 开始--运行---输入cmd---输入ping IP&#xff08;IP为所要ping的服务器的IP&#xff09; 常与 -t 选项结合使用 ctrlc结束 延时主要看时间列 看时间得数值和波动 丢包 ---出现请求超时 2.追踪路由 tracert IP 注意&#xff1a; 追路由 --一般追3次 …