五分钟完全弄懂C#特性

开发 后端
在工作或者学习中,难免或多或少的接触到特性这个东西,可能你不太清楚什么是特性,那么我给大家举两个例子 [Obsolete],[HttpGet],[HttpPost],[Serizlized],[AuthorizeFilter] (总有你见过的一个吧) 。

[[403900]]

前言

在工作或者学习中,难免或多或少的接触到特性这个东西,可能你不太清楚什么是特性,那么我给大家举两个例子 [Obsolete],[HttpGet],[HttpPost],[Serizlized],[AuthorizeFilter] (总有你见过的一个吧) 。有没有觉得好熟悉,下面跟着小赵一探究竟。

特性(Attribute)用于添加元数据,如编译器指令和注释、描述、方法、类等其他信息。

特性(Attribute)的名称和值是在方括号内规定的,放置在它所应用的元素之前。positional_parameters 规定必需的信息,name_parameter 规定可选的信息。

特性的定义

特性的定义:直接或者间接的继承 Attribute 类

定义完就直接可以在方法前面用 [CustomAttribute] 可以省略 Attribute 写成[Custom]

在特性类上面的特性 /// AttributeTargets.All --可以修饰的应用属性 /// AllowMultiple = true ---是否可以进行多次修饰 [AttributeUsage(AttributeTargets.All,AllowMultiple = true)]图片

特性的使用

特性本身是没有啥用,但是可以通过反射来使用,增加功能,不会破坏原有的封装 通过反射,发现特性 --实例化特性--使用特性 通过特性获取表名(orm)就是一个很好的案例

首先定义个类,假装和数据库中的表结构一样,但表明是t_student 可以通过两个方法来获取表名(方法1加字段,或者扩展方法tostring,但都破坏了以前的封装,不提倡这样做),然后就用到今天学习的特性attribute了

  1. public class Student 
  2.    { 
  3.         //public static string tablename = "t_student"
  4.        //public string tostring() 
  5.        //{ 
  6.        //    return "t_student"
  7.        //} 
  8.        public  int id { get; set; } 
  9.        public string Name { get; set; } 
  10.        public int Sex { get; set; } 
  11.    } 

在定义特性类TableNameAttribute

  1. //1.声明 
  2.      public  class TableNameAttribute:Attribute 
  3.      { 
  4.          private string _name = null
  5.          //初始化构造函数 
  6.         public  TableNameAttribute(string tablename) 
  7.         { 
  8.             this._name = tablename; 
  9.         } 
  10.  
  11.         public string GetTableName() 
  12.         { 
  13.             return this._name; 
  14.         } 
  15.      } 

然后再student前面加上自定义特性

实现特性的扩展方法

  1. //通过反射获取表名 
  2.        public static string GetName(Type type) 
  3.        { 
  4.            if (type.IsDefined(typeof(TableNameAttribute),true)) 
  5.            { 
  6.                TableNameAttribute attribute =(TableNameAttribute)type.GetCustomAttribute(typeof(TableNameAttribute), true); 
  7.  
  8.                return attribute.GetTableName(); 
  9.            } 
  10.            else 
  11.            { 
  12.              return  type.Name
  13.            } 
  14.        } 

F5执行,查看运行结果

 

总结

特性本身是没有啥用,但是可以通过反射来使用,增加功能,不会破坏原有的封装 项目我放在我的github[1]https://github.com/PrideJoy/NetTemple/tree/master/%E7%89%B9%E6%80%A7

 

责任编辑:武晓燕 来源: CSharp编程大全
相关推荐

2009-11-05 10:55:22

Visual Stud

2013-12-11 10:00:14

C++新特性C

2020-04-03 18:46:27

IPv6互联网IPv4

2009-11-16 10:53:30

Oracle Hint

2020-12-17 10:00:16

Python协程线程

2020-06-16 08:47:53

磁盘

2019-08-09 10:33:36

开发技能代码

2021-06-07 09:51:22

原型模式序列化

2009-11-05 14:53:54

Visual Stud

2009-10-22 16:18:19

Oracle表空间

2021-10-19 07:27:08

HTTP代理网络

2018-03-29 10:13:54

物联网协议MQTT

2009-11-16 09:53:56

PHP上传类

2021-12-01 06:50:50

Docker底层原理

2009-11-17 14:50:50

Oracle调优

2022-12-16 09:55:50

网络架构OSI

2023-07-23 18:47:59

Docker开源

2023-09-07 23:52:50

Flink代码

2024-03-28 08:43:42

webpack代码true​

2020-10-27 10:43:24

Redis字符串数据库
点赞
收藏

51CTO技术栈公众号