好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

一文说通C#的属性Attribute

一、 属性

属性 Attribute s在 C# 中很常用,但事实上很多人对这个东西又很陌生。

从概念上讲,属性提供的是将元数据关系到元素的一种方式。

属性使用的样子,应该都见过:

[Flags] //Attribute  public  enum DayOfWeek  {      Sunday = 1,      Monday = 2,      Tuesday = 4,      Wednesday = 8,      Thursday = 16,      Friday = 32,      Saturday = 64  } 

代码中,Flags就是一个属性。

通常,属性会放在类、字段、方法等定义的上面,用来指定特定的内容。

.Net Framework框架提供了一些属性。像常见的Serializable,用来告诉编译器当前的类可以序列化成JSON或XML:

[ Serializable ]  public  class SerializableClass { /*...*/ } 

需要注意的是,属性在编译时会嵌入到程序集中。这样,我们可以使用反射来获得相应的属性值。

二、自定义属性

自定义属性用处很大,算是我自己比较常用的一个技术。

自定义属性需要从System.Attribute抽象类来继承。

想象一个场景。我们在构建一个手机类。我们需要一个属性来表示手机一些信息,比方口牌和生产年份:

public  class MobileInformationAttribute : Attribute  {       public  string brand { get;  set ; }       public   int  yearOfProduct { get;  set ; }         public  MobileInformationAttribute(string Brand,  int  YearOfProduct)      {          brand = Brand;          yearOfProduct = YearOfProduct;      }  } 

我们会注意到:属性是一个类,和其它类一样,拥有字段、方法、构造函数和其它成员。

三、使用属性

前面说了,属性可以放在类、字段、方法等定义的上面。

我们来看看上面这个自定义属性的使用:

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

这儿需要注意一下:对于自定义属性的名字,如果我们采用xxx+Attribute的名称,则使用时我们可以用短名称xxx。否则,就需要使用完整的名称:

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

四、限制属性

属性本身也是一个类。所以属性也可以用属性来指定和修饰。

在修饰属性的属性中,有一个框架中的属性用的很多,就是AttributeUsage。这个属性用来限制自定义属性可以修饰的元素类型:

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

AttributeTargets是一个枚举,有很多选项,包括类、接口、方法、构造函数、枚举、程序集等。

上边的代码,我们限定了属性只用于指定和修饰类和接口。所以,如果用这个属性来修饰一个字段,编译器会报错。

AttributeUsage还允许我们定义从修饰对象继承的对象,是否也获得属性:

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

以及该属性是否可以在一个元素上有多个实例:

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

五、访问属性

有了属性,怎么访问呢?

框架提供了一个方法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}" ); 

六、反射访问

反射最主要的作用,是用来收集对象的数据,而不是对象本身的数据。这些数据包括对象的类型,以及关于对象成员(包括方法、属性、构造函数)的信息,和关于特定程序集的信息。此外,还包括存储在元素属性中的任何信息。

最简单的反射,就是GetType()方法。

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

除此之外,我们还可以使用反射来获取关于包含给定类型的程序集的信息:

Assembly assembly = typeof(DateTime).Assembly;  Console.WriteLine(assembly);    Assembly mobileAssembly = typeof(IPhone12).Assembly;  Console.WriteLine(mobileAssembly); 

关于反射的内容,不展开讨论。

这儿说的,是通过反射获取类中方法的信息:

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) 

同样,还可能通过反射获得关于已定义的属性的信息,以及关于对象的构造函数的信息:

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() 

七、使用反射创建实例

这个需要用到system.Activator。这是一个非常强大的类,可以从类型创建对象的实例。

来看看这个方法的使用:

ReflectedClass newReflected = new ReflectedClass();    var reflectedType = newReflected.GetType();    object newObject = Activator.CreateInstance(reflectedType);  Console.WriteLine(newObject); 

八、使用反射处理泛型

使用反射处理泛型会比处理普通类型麻烦一点。

这里需要知道,Type类上有一个属性用来标识类型是不是泛型:

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

同样,我们也可以用反射来创建一个泛型的实例:

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()); 

有一点复杂,但可以实现。

九、总结

写得好像有点乱。

总结一下,属性将元数据分配给元素,包括类、字段、方法等等。该元数据在构建项目时被编译,并描述元素,而不是元素的数据。

可以创建从Attribute类继承的自定义属性。可以使用AttributeUsage属性来限制这些属性的使用位置,并且可以使用反射来获取属性数据。

反射是一种技术,允许获取关于元素(而不是元素本身)的元数据和信息。执行反射的最基本方法是使用GetType()方法,但是也可以使用反射来获取关于方法、构造函数、字段等的信息。

可以使用反射来创建对象的实例,只要有了对象的类型。同时,使用反射创建泛型对象是可能的,但比较复杂,需要泛型对象的类型以及所有泛型参数的类型。

原文地址:https://mp.weixin.qq.com/s/bc9trjaVHK5mj97vgt_uVg

dy("nrwz");

查看更多关于一文说通C#的属性Attribute的详细内容...

  阅读:76次