
一、属性
属性Attributes在C#中很常用,但事实上很多人对这个东西又很陌生。
从概念上讲,属性提供的是将元数据关系到元素的一种方式。
属性使用的样子,应该都见过:
- [Flags]//Attribute
- publicenumDayOfWeek
- {
- Sunday=1,
- Monday=2,
- Tuesday=4,
- Wednesday=8,
- Thursday=16,
- Friday=32,
- Saturday=64
- }
代码中,Flags就是一个属性。
通常,属性会放在类、字段、方法等定义的上面,用来指定特定的内容。
.Net Framework框架提供了一些属性。像常见的Serializable,用来告诉编译器当前的类可以序列化成JSON或XML:
- [Serializable]
- publicclassSerializableClass{/*...*/}
需要注意的是,属性在编译时会嵌入到程序集中。这样,我们可以使用反射来获得相应的属性值。
二、自定义属性
自定义属性用处很大,算是我自己比较常用的一个技术。
自定义属性需要从System.Attribute抽象类来继承。
想象一个场景。我们在构建一个手机类。我们需要一个属性来表示手机一些信息,比方口牌和生产年份:
- publicclassMobileInformationAttribute:Attribute
- {
- publicstringbrand{get;set;}
- publicintyearOfProduct{get;set;}
- publicMobileInformationAttribute(stringBrand,intYearOfProduct)
- {
- brand=Brand;
- yearOfProduct=YearOfProduct;
- }
- }
我们会注意到:属性是一个类,和其它类一样,拥有字段、方法、构造函数和其它成员。
三、使用属性
前面说了,属性可以放在类、字段、方法等定义的上面。
我们来看看上面这个自定义属性的使用:
- [MobileInformation("Apple",2021)]
- publicclassIPhone12{/*...*/}
这儿需要注意一下:对于自定义属性的名字,如果我们采用xxx+Attribute的名称,则使用时我们可以用短名称xxx。否则,就需要使用完整的名称:
- publicclassabc:Attribute{/*...*/}
- [abc("Apple",2021)]
- publicclassIPhone12{/*...*/}
四、限制属性
属性本身也是一个类。所以属性也可以用属性来指定和修饰。
在修饰属性的属性中,有一个框架中的属性用的很多,就是AttributeUsage。这个属性用来限制自定义属性可以修饰的元素类型:
- [AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)]
- publicclassMobileInformationAttribute:Attribute{/*...*/}
AttributeTargets是一个枚举,有很多选项,包括类、接口、方法、构造函数、枚举、程序集等。
上边的代码,我们限定了属性只用于指定和修饰类和接口。所以,如果用这个属性来修饰一个字段,编译器会报错。
AttributeUsage还允许我们定义从修饰对象继承的对象,是否也获得属性:
- [AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface,Inherited=true)]
- publicclassMobileInformationAttribute:Attribute{/*...*/}
以及该属性是否可以在一个元素上有多个实例:
- [AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface,AllowMultiple=false)]
- publicclassMobileInformationAttribute:Attribute{/*...*/}
五、访问属性
有了属性,怎么访问呢?
框架提供了一个方法Attribute.GetCustomAttribute():
- varmobileType=typeof(IPhone12);
- varattributeType=typeof(MobileInformationAttribute);
- varattribute=(MobileInformationAttribute)Attribute.GetCustomAttribute(mobileType,attributeType);
- Console.WriteLine($"Mobileis{attribute.brand}{attribute.yearOfProduct}");
六、反射访问
反射最主要的作用,是用来收集对象的数据,而不是对象本身的数据。这些数据包括对象的类型,以及关于对象成员(包括方法、属性、构造函数)的信息,和关于特定程序集的信息。此外,还包括存储在元素属性中的任何信息。
最简单的反射,就是GetType()方法。
- intmyInt=5;
- Typetype=myInt.GetType();
- Console.WriteLine(type);
除此之外,我们还可以使用反射来获取关于包含给定类型的程序集的信息:
- Assemblyassembly=typeof(DateTime).Assembly;
- Console.WriteLine(assembly);
- AssemblymobileAssembly=typeof(IPhone12).Assembly;
- Console.WriteLine(mobileAssembly);
关于反射的内容,不展开讨论。
这儿说的,是通过反射获取类中方法的信息:
- publicclassReflectedClass
- {
- publicstringProperty1{get;set;}
- publicintAdd(intfirst,intsecond)
- {
- returnfirst+second;
- }
- }
- ReflectedClassreflected=newReflectedClass();
- MemberInfomember=reflected.GetType().GetMethod("Add");
- Console.WriteLine(member);//Int32Add(Int32,Int32)
同样,还可能通过反射获得关于已定义的属性的信息,以及关于对象的构造函数的信息:
- PropertyInfoproperty=reflected.GetType().GetProperty("Property1");
- Console.WriteLine(property);//System.StringProperty1
- ConstructorInfoconstructor=reflected.GetType().GetConstructor(newType[0]);
- Console.WriteLine(constructor);//Void.ctor()
七、使用反射创建实例
这个需要用到system.Activator。这是一个非常强大的类,可以从类型创建对象的实例。
来看看这个方法的使用:
- ReflectedClassnewReflected=newReflectedClass();
- varreflectedType=newReflected.GetType();
- objectnewObject=Activator.CreateInstance(reflectedType);
- Console.WriteLine(newObject);
八、使用反射处理泛型
使用反射处理泛型会比处理普通类型麻烦一点。
这里需要知道,Type类上有一个属性用来标识类型是不是泛型:
- List<int>numbers=newList<int>{1,2,3,4,5,6,7};
- Console.WriteLine(numbers.GetType().IsGenericType);
同样,我们也可以用反射来创建一个泛型的实例:
- List<int>numbers=newList<int>{1,2,3,4,5,6,7};
- Typed=numbers.GetType().GetGenericTypeDefinition();
- Type[]typeArgs=newType[]{typeof(int)};
- Typeconstructed=d.MakeGenericType(typeArgs);
- objectlist=Activator.CreateInstance(constructed);
- Console.WriteLine(list.GetType());
有一点复杂,但可以实现。
九、总结
写得好像有点乱。
总结一下,属性将元数据分配给元素,包括类、字段、方法等等。该元数据在构建项目时被编译,并描述元素,而不是元素的数据。
可以创建从Attribute类继承的自定义属性。可以使用AttributeUsage属性来限制这些属性的使用位置,并且可以使用反射来获取属性数据。
反射是一种技术,允许获取关于元素(而不是元素本身)的元数据和信息。执行反射的最基本方法是使用GetType()方法,但是也可以使用反射来获取关于方法、构造函数、字段等的信息。
可以使用反射来创建对象的实例,只要有了对象的类型。同时,使用反射创建泛型对象是可能的,但比较复杂,需要泛型对象的类型以及所有泛型参数的类型。
原文地址:https://mp.weixin.qq.com/s/bc9trjaVHK5mj97vgt_uVg








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