自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一文說通C#的屬性Attribute

開發(fā) 后端
屬性Attributes在C#中很常用,但事實(shí)上很多人對(duì)這個(gè)東西又很陌生。從概念上講,屬性提供的是將元數(shù)據(jù)關(guān)系到元素的一種方式。

[[392994]]

一、屬性

屬性Attributes在C#中很常用,但事實(shí)上很多人對(duì)這個(gè)東西又很陌生。

從概念上講,屬性提供的是將元數(shù)據(jù)關(guān)系到元素的一種方式。

屬性使用的樣子,應(yīng)該都見過:

  1. [Flags] //Attribute 
  2. public enum DayOfWeek 
  3.     Sunday = 1, 
  4.     Monday = 2, 
  5.     Tuesday = 4, 
  6.     Wednesday = 8, 
  7.     Thursday = 16, 
  8.     Friday = 32, 
  9.     Saturday = 64 

代碼中,F(xiàn)lags就是一個(gè)屬性。

通常,屬性會(huì)放在類、字段、方法等定義的上面,用來指定特定的內(nèi)容。

.Net Framework框架提供了一些屬性。像常見的Serializable,用來告訴編譯器當(dāng)前的類可以序列化成JSON或XML:

  1. [Serializable
  2. public class SerializableClass { /*...*/ } 

需要注意的是,屬性在編譯時(shí)會(huì)嵌入到程序集中。這樣,我們可以使用反射來獲得相應(yīng)的屬性值。

二、自定義屬性

自定義屬性用處很大,算是我自己比較常用的一個(gè)技術(shù)。

自定義屬性需要從System.Attribute抽象類來繼承。

想象一個(gè)場(chǎng)景。我們?cè)跇?gòu)建一個(gè)手機(jī)類。我們需要一個(gè)屬性來表示手機(jī)一些信息,比方口牌和生產(chǎn)年份:

  1. public class MobileInformationAttribute : Attribute 
  2.     public string brand { get; set; } 
  3.     public int yearOfProduct { get; set; } 
  4.  
  5.     public MobileInformationAttribute(string Brand, int YearOfProduct) 
  6.     { 
  7.         brand = Brand; 
  8.         yearOfProduct = YearOfProduct; 
  9.     } 

我們會(huì)注意到:屬性是一個(gè)類,和其它類一樣,擁有字段、方法、構(gòu)造函數(shù)和其它成員。

三、使用屬性

前面說了,屬性可以放在類、字段、方法等定義的上面。

我們來看看上面這個(gè)自定義屬性的使用:

  1. [MobileInformation("Apple", 2021)] 
  2. public class IPhone12 { /*...*/ } 

這兒需要注意一下:對(duì)于自定義屬性的名字,如果我們采用xxx+Attribute的名稱,則使用時(shí)我們可以用短名稱xxx。否則,就需要使用完整的名稱:

  1. public class abc : Attribute { /*...*/ } 
  2.  
  3. [abc("Apple", 2021)] 
  4. public class IPhone12 { /*...*/ } 

四、限制屬性

屬性本身也是一個(gè)類。所以屬性也可以用屬性來指定和修飾。

在修飾屬性的屬性中,有一個(gè)框架中的屬性用的很多,就是AttributeUsage。這個(gè)屬性用來限制自定義屬性可以修飾的元素類型:

  1. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] 
  2. public class MobileInformationAttribute : Attribute { /*...*/ } 

AttributeTargets是一個(gè)枚舉,有很多選項(xiàng),包括類、接口、方法、構(gòu)造函數(shù)、枚舉、程序集等。

上邊的代碼,我們限定了屬性只用于指定和修飾類和接口。所以,如果用這個(gè)屬性來修飾一個(gè)字段,編譯器會(huì)報(bào)錯(cuò)。

AttributeUsage還允許我們定義從修飾對(duì)象繼承的對(duì)象,是否也獲得屬性:

  1. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, Inherited = true)] 
  2. public class MobileInformationAttribute : Attribute { /*...*/ } 

以及該屬性是否可以在一個(gè)元素上有多個(gè)實(shí)例:

  1. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = false)] 
  2. public class MobileInformationAttribute : Attribute { /*...*/ } 

五、訪問屬性

有了屬性,怎么訪問呢?

框架提供了一個(gè)方法Attribute.GetCustomAttribute():

  1. var mobileType = typeof(IPhone12); 
  2. var attributeType = typeof(MobileInformationAttribute); 
  3. var attribute = (MobileInformationAttribute)Attribute.GetCustomAttribute(mobileType, attributeType); 
  4. Console.WriteLine($"Mobile is {attribute.brand} {attribute.yearOfProduct}"); 

六、反射訪問

反射最主要的作用,是用來收集對(duì)象的數(shù)據(jù),而不是對(duì)象本身的數(shù)據(jù)。這些數(shù)據(jù)包括對(duì)象的類型,以及關(guān)于對(duì)象成員(包括方法、屬性、構(gòu)造函數(shù))的信息,和關(guān)于特定程序集的信息。此外,還包括存儲(chǔ)在元素屬性中的任何信息。

最簡單的反射,就是GetType()方法。

  1. int myInt = 5; 
  2. Type type = myInt.GetType(); 
  3. Console.WriteLine(type); 

除此之外,我們還可以使用反射來獲取關(guān)于包含給定類型的程序集的信息:

  1. Assembly assembly = typeof(DateTime).Assembly; 
  2. Console.WriteLine(assembly); 
  3.  
  4. Assembly mobileAssembly = typeof(IPhone12).Assembly; 
  5. Console.WriteLine(mobileAssembly); 

關(guān)于反射的內(nèi)容,不展開討論。

這兒說的,是通過反射獲取類中方法的信息:

  1. public class ReflectedClass 
  2.     public string Property1 { get; set; } 
  3.  
  4.     public int Add(int firstint second
  5.     { 
  6.         return first + second
  7.     } 
  8.  
  9. ReflectedClass reflected = new ReflectedClass(); 
  10. MemberInfo member = reflected.GetType().GetMethod("Add"); 
  11. Console.WriteLine(member); //Int32 Add(Int32, Int32) 

同樣,還可能通過反射獲得關(guān)于已定義的屬性的信息,以及關(guān)于對(duì)象的構(gòu)造函數(shù)的信息:

  1. PropertyInfo property = reflected.GetType().GetProperty("Property1"); 
  2. Console.WriteLine(property); //System.String Property1 
  3.  
  4. ConstructorInfo constructor = reflected.GetType().GetConstructor(new Type[0]); 
  5. Console.WriteLine(constructor); //Void .ctor() 

七、使用反射創(chuàng)建實(shí)例

這個(gè)需要用到system.Activator。這是一個(gè)非常強(qiáng)大的類,可以從類型創(chuàng)建對(duì)象的實(shí)例。

來看看這個(gè)方法的使用:

  1. ReflectedClass newReflected = new ReflectedClass(); 
  2.  
  3. var reflectedType = newReflected.GetType(); 
  4.  
  5. object newObject = Activator.CreateInstance(reflectedType); 
  6. Console.WriteLine(newObject); 

八、使用反射處理泛型

使用反射處理泛型會(huì)比處理普通類型麻煩一點(diǎn)。

這里需要知道,Type類上有一個(gè)屬性用來標(biāo)識(shí)類型是不是泛型:

  1. List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7 }; 
  2. Console.WriteLine(numbers.GetType().IsGenericType); 

同樣,我們也可以用反射來創(chuàng)建一個(gè)泛型的實(shí)例:

  1. List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7 }; 
  2.  
  3. Type d = numbers.GetType().GetGenericTypeDefinition(); 
  4.  
  5. Type[] typeArgs = new Type[] { typeof(int) }; 
  6.  
  7. Type constructed = d.MakeGenericType(typeArgs); 
  8.  
  9. object list = Activator.CreateInstance(constructed); 
  10.  
  11. Console.WriteLine(list.GetType()); 

有一點(diǎn)復(fù)雜,但可以實(shí)現(xiàn)。

九、總結(jié)

寫得好像有點(diǎn)亂。

總結(jié)一下,屬性將元數(shù)據(jù)分配給元素,包括類、字段、方法等等。該元數(shù)據(jù)在構(gòu)建項(xiàng)目時(shí)被編譯,并描述元素,而不是元素的數(shù)據(jù)。

可以創(chuàng)建從Attribute類繼承的自定義屬性??梢允褂肁ttributeUsage屬性來限制這些屬性的使用位置,并且可以使用反射來獲取屬性數(shù)據(jù)。

反射是一種技術(shù),允許獲取關(guān)于元素(而不是元素本身)的元數(shù)據(jù)和信息。執(zhí)行反射的最基本方法是使用GetType()方法,但是也可以使用反射來獲取關(guān)于方法、構(gòu)造函數(shù)、字段等的信息。

 

可以使用反射來創(chuàng)建對(duì)象的實(shí)例,只要有了對(duì)象的類型。同時(shí),使用反射創(chuàng)建泛型對(duì)象是可能的,但比較復(fù)雜,需要泛型對(duì)象的類型以及所有泛型參數(shù)的類型。

 

責(zé)任編輯:武曉燕 來源: 老王Plus
相關(guān)推薦

2021-09-15 06:55:34

異步LinqC#

2021-01-27 08:12:04

Dotnet函數(shù)數(shù)據(jù)

2022-04-28 10:41:08

SaaS業(yè)務(wù)方式

2019-11-12 15:11:45

秒殺流量高可用

2021-07-31 23:14:26

OpenCL框架語言

2021-12-15 09:32:41

Linux系統(tǒng)負(fù)載

2025-04-22 08:57:27

2018-05-22 10:09:09

數(shù)據(jù)庫MySQL優(yōu)化原理

2024-04-28 08:14:29

C#隊(duì)列Queue

2024-05-30 08:05:17

2019-01-29 09:36:10

MySQLACID特性

2020-05-11 07:57:33

區(qū)塊鏈分布式鏈上

2023-01-26 01:09:31

配置數(shù)據(jù)源參數(shù)

2024-02-22 14:20:44

數(shù)字化轉(zhuǎn)型數(shù)字化

2009-08-03 14:42:50

C#自定義控件

2009-08-04 14:25:09

學(xué)習(xí)C#Attribute與P

2009-09-11 11:16:53

C# Attribut

2020-01-22 16:50:32

區(qū)塊鏈技術(shù)智能

2022-10-08 06:38:01

元宇宙NFT加密貨幣

2025-01-14 17:00:00

SpringBoot開發(fā)代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)