一文说通C#的属性Attribute

开发 后端
属性Attributes在C#中很常用,但事实上很多人对这个东西又很陌生。从概念上讲,属性提供的是将元数据关系到元素的一种方式。

[[392994]]

一、属性

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

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

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

  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 

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

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

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

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

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

二、自定义属性

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

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

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

  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.     } 

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

三、使用属性

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

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

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

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

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

四、限制属性

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

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

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

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

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

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

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

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

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

五、访问属性

有了属性,怎么访问呢?

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

六、反射访问

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

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

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

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

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

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

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

  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) 

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

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

七、使用反射创建实例

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

来看看这个方法的使用:

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

八、使用反射处理泛型

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

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

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

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

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

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

九、总结

写得好像有点乱。

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

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

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

 

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

 

责任编辑:武晓燕 来源: 老王Plus
相关推荐

2021-01-27 08:12:04

Dotnet函数数据

2021-09-15 06:55:34

异步LinqC#

2022-04-28 10:41:08

SaaS业务方式

2019-11-12 15:11:45

秒杀流量高可用

2021-07-31 23:14:26

OpenCL框架语言

2021-12-15 09:32:41

Linux系统负载

2018-05-22 10:09:09

数据库MySQL优化原理

2019-01-29 09:36:10

MySQLACID特性

2020-05-11 07:57:33

区块链分布式链上

2024-02-22 14:20:44

数字化转型数字化

2023-01-26 01:09:31

配置数据源参数

2009-08-03 14:42:50

C#自定义控件

2020-01-22 16:50:32

区块链技术智能

2009-08-04 14:25:09

学习C#Attribute与P

2009-09-11 11:16:53

C# Attribut

2022-10-08 06:38:01

元宇宙NFT加密货币

2009-09-01 17:41:45

C# HelpAttr

2021-09-09 17:05:36

C++智能指针语言

2009-09-17 17:13:54

C#数组

2021-09-07 05:02:50

C++ConstexprConst
点赞
收藏

51CTO技术栈公众号