c#中的特性
创始人
2024-12-29 09:40:50
0

在C#中,特性(Attributes)是一种向程序元素(如类、方法、属性等)添加元数据的方式。特性可以用来提供关于程序元素的附加信息,这些信息可以在编译时和运行时被访问。

特性主要有以下几个用途:

  1. 提供元数据:特性可以提供关于类、方法、属性等的附加信息,这些信息可以被编译器、运行时环境或其他工具使用。

  2. 标记程序元素:特性可以用来标记程序元素,以便在编译时或运行时进行特定的处理。例如,使用 [Obsolete] 特性可以标记一个方法或类为已过时。

  3. 自定义行为:开发者可以创建自定义特性,以提供特定的行为或功能。例如,通过自定义特性,可以在运行时动态地改变程序的行为。

  4. 支持反射:特性与反射紧密集成,使得程序可以在运行时查询和使用特性提供的信息。

  5. 简化代码:使用特性可以简化代码,避免重复代码,提高代码的可维护性和可读性。

C#中一些常用的内置特性包括:

  • [Obsolete]:用于标记类、方法、属性等为已过时,如果代码中使用了这些元素,编译器会发出警告。

  • [Serializable]:指示一个类可以被序列化,这对于跨应用程序域或网络传输对象非常有用。

  • [AttributeUsage]:定义自定义特性的使用方式,例如它可以应用于类、方法或属性。

  • [Flags]:用于标记枚举,表示枚举可以作为一组标志来使用,通常与位运算结合使用。

  • [Conditional]:用于条件编译,只有当定义了特定的预处理器符号时,标记的方法才会被调用。

using System.Diagnostics; ​ namespace _7._14day01 {     internal class Program     {         static void Main(string[] args)         {             UserClass userClass = new UserClass();             userClass.print(); ​         }          ​     }     public class UserClass     {         [DebuggerStepThrough]         public  void print()         { ​             Console.WriteLine("这个方法很简单");         }     } } ​

能够直接跳过该方法的Debug

using System.Diagnostics; ​ namespace _7._14day01 {     internal class Program     {         static void Main(string[] args)         {             UserClass userClass = new UserClass();             userClass.print(); ​         }          ​     }     public class UserClass     {                 [Obsolete("1111", true)]         public  void print()         { ​             Console.WriteLine("这个方法很简单");         }     } } ​

当将[Obsolete]标记为true时 如果使用该类 系统会报错

使用的地方

几乎所有的框架都用到了,MVC---WebApi----EF-----IOC----AOP

特性的使用场景:可以用来做数据验证。

特性分类

第一种C#自带

第二种 自定义

使用方法如下

using System.Diagnostics; ​ namespace _7._14day01 {     internal class Program     {         static void Main(string[] args)         {             //UserClass userClass = new UserClass();             //userClass.print();             //UseAttribute useAttribute = new UseAttribute();             AttributeTest attributeTest = new AttributeTest();             attributeTest.Test(); ​         } ​ ​     } ​     class AttributeTest     {         public void Test()         {             Type type = typeof(UseAttribute);             object[] obj = type.GetCustomAttributes(true);             foreach (object customArributes in obj)             {                 DefindAttribute defindAttribute = customArributes as DefindAttribute;                 if(defindAttribute != null)                 {                     Console.WriteLine(defindAttribute.Showinfo);                 }             }         }     } ​     [DefindAttribute("调用了这个特性DefindAttribute")]     class UseAttribute     { ​ ​ ​     }     class DefindAttribute : Attribute     {         public DefindAttribute(string showinfo)         {             Showinfo = showinfo; ​         }         public string Showinfo { get; set; }     } ​ ​ } ​

相关内容

热门资讯

专业辅助!微乐小程序自建房插件... 《专业辅助!微乐小程序自建房插件免费,微信边锋辅助工具(起初是真的有挂)》 微乐小程序自建房插件免费...
玩家必看教程!微乐陕西小程序破... 玩家必看教程!微乐陕西小程序破解器下载(开挂辅助教程)一直有透视经验1、每一步都需要思考,不同水平的...
推荐辅助!2025微乐小程序黑... 推荐辅助!2025微乐小程序黑科技,微信多乐辅助(一向有挂);推荐辅助!2025微乐小程序黑科技,微...
实测揭晓!潮汕潮汕娱脚本(揭露... 实测揭晓!潮汕潮汕娱脚本(揭露辅助神器)切实有透视教材1、打开软件启动之后找到中间准星的标志长按。2...
专业辅助!微乐小程序黑科技下载... 专业辅助!微乐小程序黑科技下载ios,蜀山四川破解版安卓版(一贯是真的有挂);1、很好的微乐小程序黑...
分享开挂内幕!微信广东雀神挂件... 分享开挂内幕!微信广东雀神挂件辅助(解迷辅助软件)总是有透视窍门1、下载好微信广东雀神挂件辅助脚本下...
发现辅助!微乐小程序辅助脚本,... 发现辅助!微乐小程序辅助脚本,广西老友麻将有挂吗(确实有挂),微乐小程序辅助脚本是用手机号来登录游戏...
揭秘真相!凑一桌游戏关春天辅助... 揭秘真相!凑一桌游戏关春天辅助(解谜辅助app)果然有透视窍门1、凑一桌游戏关春天辅助模拟器是什么优...
专业辅助!微乐小程序黑科技下载... 专业辅助!微乐小程序黑科技下载ios,微信牵手跑辅助下载(确实存在有挂);最新版2026是一款经典耐...
今日头条!卡农血拼辅助(必备辅... 今日头条!卡农血拼辅助(必备辅助攻略)原来有透视手筋亲,关键说明,卡农血拼辅助透视脚本安卓赛季回归,...