一文說(shuō)通C#的屬性Attribute
一、屬性
屬性Attributes在C#中很常用,但事實(shí)上很多人對(duì)這個(gè)東西又很陌生。
從概念上講,屬性提供的是將元數(shù)據(jù)關(guān)系到元素的一種方式。
屬性使用的樣子,應(yīng)該都見(jiàn)過(guò):
- [Flags] //Attribute
 - public enum DayOfWeek
 - {
 - Sunday = 1,
 - Monday = 2,
 - Tuesday = 4,
 - Wednesday = 8,
 - Thursday = 16,
 - Friday = 32,
 - Saturday = 64
 - }
 
代碼中,F(xiàn)lags就是一個(gè)屬性。
通常,屬性會(huì)放在類、字段、方法等定義的上面,用來(lái)指定特定的內(nèi)容。
.Net Framework框架提供了一些屬性。像常見(jiàn)的Serializable,用來(lái)告訴編譯器當(dāng)前的類可以序列化成JSON或XML:
- [Serializable]
 - public class SerializableClass { /*...*/ }
 
需要注意的是,屬性在編譯時(shí)會(huì)嵌入到程序集中。這樣,我們可以使用反射來(lái)獲得相應(yīng)的屬性值。
二、自定義屬性
自定義屬性用處很大,算是我自己比較常用的一個(gè)技術(shù)。
自定義屬性需要從System.Attribute抽象類來(lái)繼承。
想象一個(gè)場(chǎng)景。我們?cè)跇?gòu)建一個(gè)手機(jī)類。我們需要一個(gè)屬性來(lái)表示手機(jī)一些信息,比方口牌和生產(chǎn)年份:
- public class MobileInformationAttribute : Attribute
 - {
 - public string brand { get; set; }
 - public int yearOfProduct { get; set; }
 - public MobileInformationAttribute(string Brand, int YearOfProduct)
 - {
 - brand = Brand;
 - yearOfProduct = YearOfProduct;
 - }
 - }
 
我們會(huì)注意到:屬性是一個(gè)類,和其它類一樣,擁有字段、方法、構(gòu)造函數(shù)和其它成員。
三、使用屬性
前面說(shuō)了,屬性可以放在類、字段、方法等定義的上面。
我們來(lái)看看上面這個(gè)自定義屬性的使用:
- [MobileInformation("Apple", 2021)]
 - public class IPhone12 { /*...*/ }
 
這兒需要注意一下:對(duì)于自定義屬性的名字,如果我們采用xxx+Attribute的名稱,則使用時(shí)我們可以用短名稱xxx。否則,就需要使用完整的名稱:
- public class abc : Attribute { /*...*/ }
 - [abc("Apple", 2021)]
 - public class IPhone12 { /*...*/ }
 
四、限制屬性
屬性本身也是一個(gè)類。所以屬性也可以用屬性來(lái)指定和修飾。
在修飾屬性的屬性中,有一個(gè)框架中的屬性用的很多,就是AttributeUsage。這個(gè)屬性用來(lái)限制自定義屬性可以修飾的元素類型:
- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
 - public class MobileInformationAttribute : Attribute { /*...*/ }
 
AttributeTargets是一個(gè)枚舉,有很多選項(xiàng),包括類、接口、方法、構(gòu)造函數(shù)、枚舉、程序集等。
上邊的代碼,我們限定了屬性只用于指定和修飾類和接口。所以,如果用這個(gè)屬性來(lái)修飾一個(gè)字段,編譯器會(huì)報(bào)錯(cuò)。
AttributeUsage還允許我們定義從修飾對(duì)象繼承的對(duì)象,是否也獲得屬性:
- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, Inherited = true)]
 - public class MobileInformationAttribute : Attribute { /*...*/ }
 
以及該屬性是否可以在一個(gè)元素上有多個(gè)實(shí)例:
- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = false)]
 - public class MobileInformationAttribute : Attribute { /*...*/ }
 
五、訪問(wèn)屬性
有了屬性,怎么訪問(wèn)呢?
框架提供了一個(gè)方法Attribute.GetCustomAttribute():
- var mobileType = typeof(IPhone12);
 - var attributeType = typeof(MobileInformationAttribute);
 - var attribute = (MobileInformationAttribute)Attribute.GetCustomAttribute(mobileType, attributeType);
 - Console.WriteLine($"Mobile is {attribute.brand} {attribute.yearOfProduct}");
 
六、反射訪問(wèn)
反射最主要的作用,是用來(lái)收集對(duì)象的數(shù)據(jù),而不是對(duì)象本身的數(shù)據(jù)。這些數(shù)據(jù)包括對(duì)象的類型,以及關(guān)于對(duì)象成員(包括方法、屬性、構(gòu)造函數(shù))的信息,和關(guān)于特定程序集的信息。此外,還包括存儲(chǔ)在元素屬性中的任何信息。
最簡(jiǎn)單的反射,就是GetType()方法。
- int myInt = 5;
 - Type type = myInt.GetType();
 - Console.WriteLine(type);
 
除此之外,我們還可以使用反射來(lái)獲取關(guān)于包含給定類型的程序集的信息:
- Assembly assembly = typeof(DateTime).Assembly;
 - Console.WriteLine(assembly);
 - Assembly mobileAssembly = typeof(IPhone12).Assembly;
 - Console.WriteLine(mobileAssembly);
 
關(guān)于反射的內(nèi)容,不展開(kāi)討論。
這兒說(shuō)的,是通過(guò)反射獲取類中方法的信息:
- public class ReflectedClass
 - {
 - public string Property1 { get; set; }
 - public int Add(int first, int second)
 - {
 - return first + second;
 - }
 - }
 - ReflectedClass reflected = new ReflectedClass();
 - MemberInfo member = reflected.GetType().GetMethod("Add");
 - Console.WriteLine(member); //Int32 Add(Int32, Int32)
 
同樣,還可能通過(guò)反射獲得關(guān)于已定義的屬性的信息,以及關(guān)于對(duì)象的構(gòu)造函數(shù)的信息:
- PropertyInfo property = reflected.GetType().GetProperty("Property1");
 - Console.WriteLine(property); //System.String Property1
 - ConstructorInfo constructor = reflected.GetType().GetConstructor(new Type[0]);
 - Console.WriteLine(constructor); //Void .ctor()
 
七、使用反射創(chuàng)建實(shí)例
這個(gè)需要用到system.Activator。這是一個(gè)非常強(qiáng)大的類,可以從類型創(chuàng)建對(duì)象的實(shí)例。
來(lái)看看這個(gè)方法的使用:
- ReflectedClass newReflected = new ReflectedClass();
 - var reflectedType = newReflected.GetType();
 - object newObject = Activator.CreateInstance(reflectedType);
 - Console.WriteLine(newObject);
 
八、使用反射處理泛型
使用反射處理泛型會(huì)比處理普通類型麻煩一點(diǎn)。
這里需要知道,Type類上有一個(gè)屬性用來(lái)標(biāo)識(shí)類型是不是泛型:
- List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
 - Console.WriteLine(numbers.GetType().IsGenericType);
 
同樣,我們也可以用反射來(lái)創(chuàng)建一個(gè)泛型的實(shí)例:
- List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
 - Type d = numbers.GetType().GetGenericTypeDefinition();
 - Type[] typeArgs = new Type[] { typeof(int) };
 - Type constructed = d.MakeGenericType(typeArgs);
 - object list = Activator.CreateInstance(constructed);
 - 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類繼承的自定義屬性。可以使用AttributeUsage屬性來(lái)限制這些屬性的使用位置,并且可以使用反射來(lái)獲取屬性數(shù)據(jù)。
反射是一種技術(shù),允許獲取關(guān)于元素(而不是元素本身)的元數(shù)據(jù)和信息。執(zhí)行反射的最基本方法是使用GetType()方法,但是也可以使用反射來(lái)獲取關(guān)于方法、構(gòu)造函數(shù)、字段等的信息。
可以使用反射來(lái)創(chuàng)建對(duì)象的實(shí)例,只要有了對(duì)象的類型。同時(shí),使用反射創(chuàng)建泛型對(duì)象是可能的,但比較復(fù)雜,需要泛型對(duì)象的類型以及所有泛型參數(shù)的類型。















 
 
 











 
 
 
 