当前位置:首页 > 通信资讯 > 正文

C#的属性(c#属性有什么用)

C#的属性(c#属性有什么用)

一、属性

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

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

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

  1. [Flags]//Attribute
  2. publicenumDayOfWeek
  3. {
  4. Sunday=1,
  5. Monday=2,
  6. Tuesday=4,
  7. Wednesday=8,
  8. Thursday=16,
  9. Friday=32,
  10. Saturday=64
  11. }

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

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

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

  1. [Serializable]
  2. publicclassSerializableClass{/*...*/}

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

二、自定义属性

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

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

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

  1. publicclassMobileInformationAttribute:Attribute
  2. {
  3. publicstringbrand{get;set;}
  4. publicintyearOfProduct{get;set;}
  5. publicMobileInformationAttribute(stringBrand,intYearOfProduct)
  6. {
  7. brand=Brand;
  8. yearOfProduct=YearOfProduct;
  9. }
  10. }

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

三、使用属性

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

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

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

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

  1. publicclassabc:Attribute{/*...*/}
  2. [abc("Apple",2021)]
  3. publicclassIPhone12{/*...*/}

四、限制属性

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

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

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

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

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

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

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

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

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

五、访问属性

有了属性,怎么访问呢?

框架提供了一个方法Attribute.GetCustomAttribute():

  1. varmobileType=typeof(IPhone12);
  2. varattributeType=typeof(MobileInformationAttribute);
  3. varattribute=(MobileInformationAttribute)Attribute.GetCustomAttribute(mobileType,attributeType);
  4. Console.WriteLine($"Mobileis{attribute.brand}{attribute.yearOfProduct}");

六、反射访问

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

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

  1. intmyInt=5;
  2. Typetype=myInt.GetType();
  3. Console.WriteLine(type);

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

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

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

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

  1. publicclassReflectedClass
  2. {
  3. publicstringProperty1{get;set;}
  4. publicintAdd(intfirst,intsecond)
  5. {
  6. returnfirst+second;
  7. }
  8. }
  9. ReflectedClassreflected=newReflectedClass();
  10. MemberInfomember=reflected.GetType().GetMethod("Add");
  11. Console.WriteLine(member);//Int32Add(Int32,Int32)

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

  1. PropertyInfoproperty=reflected.GetType().GetProperty("Property1");
  2. Console.WriteLine(property);//System.StringProperty1
  3. ConstructorInfoconstructor=reflected.GetType().GetConstructor(newType[0]);
  4. Console.WriteLine(constructor);//Void.ctor()

七、使用反射创建实例

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

来看看这个方法的使用:

  1. ReflectedClassnewReflected=newReflectedClass();
  2. varreflectedType=newReflected.GetType();
  3. objectnewObject=Activator.CreateInstance(reflectedType);
  4. Console.WriteLine(newObject);

八、使用反射处理泛型

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

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

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

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

  1. List<int>numbers=newList<int>{1,2,3,4,5,6,7};
  2. Typed=numbers.GetType().GetGenericTypeDefinition();
  3. Type[]typeArgs=newType[]{typeof(int)};
  4. Typeconstructed=d.MakeGenericType(typeArgs);
  5. objectlist=Activator.CreateInstance(constructed);
  6. Console.WriteLine(list.GetType());

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

九、总结

写得好像有点乱。

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

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

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

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

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

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。