c#属性Property,特性类Attribute

分手后的思念是犯贱 2024-02-17 22:31 95阅读 0赞

一、Property(属性)

对类来说:属性成员描述的是状态信息,对类的实例来说,属性成员的值表示的是该对象的状态值。

1、如果类Z是抽象的,包含它的类A 也应该是抽象的。

2、重写属性的声明必须与指定与所继承的属性相同的修饰符,类型等

3、如果被继承的属性只有单个访问器(读或者写),重写属性也只能单个访问器。如果被继承的属性有两个访问器,重写属性可以只包含一个,也可包含两个

4、重写属性可以包含seal修饰符,此修饰符可以防止派生类重写该属性

二、Attribute(特性)类

为实体类型(类、接口、方法、属性)描述附加信息的方法。一旦代码的实体被附加了信息,就可以在运行时获得这些信息。特性与程序实体关联后,即可在运行时使用名为“反射”的技术查询特性。

Attribute本质上就是一个类,派生于System.Attribute类。它附着在目标对象上最终实例化

  1. 不通过new操作符来产生实例,而是使用在方括号里调用构造方法来产生实例
  2. 方括号必须紧挨着放置在被附着目标的前面。
  3. 因为方括号里空间有限,所以不能使用对象初始化器给对象的属性(Property)赋值,必须使用含参构造方法对Attribute实例中的属性赋值。

三、使用方法

1、 定义Attribute类

2、 使用Attribute类

3、 查询Attribute类

使用格式:放在[ ]即可

[显示说明符:特征列表]

也可以使用带参数的Attribute类 :[Attribute类名(位置参数表,命名参数表)]

位置参数表:即构造函数的参数

命名参数表:Attribute实例中的属性的赋值

查询代码实体成员上的特征信息,AttributeUsage有三个属性,我们可以把它放置在定制属性前面。

第一个属性:ValidOn,通过这个属性,我们能够定义定制特性应该在何种程序实体前放置

第二个属性: AllowMultiple ,这个属性标记了我们的定制特性能否被重复放置在同一个程序实体前多次

第三个属性是: Inherited ,我们可以使用这个属性来控制定制特性的继承规则。它标记了当特性被放置在一个基类上时,它能否被派生类所继承。

例子:

  1. //自定义Attribute类
  2. // This attribute is only valid on a class.
  3. [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method),
  4. AllowMultiple=true,
  5. Inherit=false]
  6. public class ClassTargetAttribute : Attribute {
  7. }
  8. // This attribute is only valid on a method.
  9. [AttributeUsage(AttributeTargets.Method)]
  10. public class MethodTargetAttribute : Attribute {
  11. }
  12. // This attribute is only valid on a constructor.
  13. [AttributeUsage(AttributeTargets.Constructor)]
  14. public class ConstructorTargetAttribute : Attribute {
  15. }
  16. // This attribute is only valid on a field.
  17. [AttributeUsage(AttributeTargets.Field)]
  18. public class FieldTargetAttribute : Attribute {
  19. }
  20. // This attribute is valid on a class or a method.
  21. [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
  22. public class ClassMethodTargetAttribute : Attribute {
  23. }
  24. // This attribute is valid on a generic type parameter.
  25. [AttributeUsage(AttributeTargets.GenericParameter)]
  26. public class GenericParameterTargetAttribute : Attribute {
  27. }
  28. [ClassTarget]//系统会自动添加Attribute
  29. [ClassMethodTarget]//系统会自动添加Attribute
  30. [AllTargets]
  31. public class TestClassAttribute {
  32. [ConstructorTarget]
  33. [AllTargets]
  34. TestClassAttribute() {
  35. }
  36. [MethodTarget]
  37. [ClassMethodTarget]
  38. [AllTargets]
  39. public void Method1() {
  40. }
  41. [FieldTarget]
  42. [AllTargets]
  43. public int myInt;
  44. // This attribute is valid on any target.
  45. [AttributeUsage(AttributeTargets.All)]
  46. public class AllTargetsAttribute : Attribute {
  47. }

注意:对一个特性类名使用Attribute后缀是一个惯例,也可不添加,系统会自动添加。

Attribute类的查询

1、使用GetCustomAttribute或者GetCustomAttributes方法查询,查询指定代码实体上指定特征类型

2、使用反射机制查询

示例

第一个例子,我们通过特性类Attribute,来对类、以及类成员进行进一步的描述。

比如我们写一个关于人的类Person,该类可以对人的属性以及某些行为(方法)进行描述。那么如果我们要对人类进行进一步描述呢,比如人这个类是属于动物的灵长类动物。有人会说我们可以为这个Person类去写一个灵长动物类的父类,再用人类去继承这个类去解决。但是我们要求的是仅仅是描述性的,就是对这个人类进行进一步的描述,而在实际操作中不需要去操作。这样的情况我们就可以用特性的概念去解决,特性简而言之就是程序集的特定程序元素所具有的另外的性质。
我们先写一个人类Person类

  1. Class Person
  2. {
  3. //人的姓名储存字段和属性
  4. private string name;
  5. public string Name
  6. {
  7. set{name=value;}
  8. get{return name;}
  9. //人的年龄储存字段和属性
  10. private int age;
  11. public int Age
  12. {
  13. set{age=value;}
  14. get{return age;}
  15. }
  16. }
  17. //人的打招呼方法
  18. public void SayHello()
  19. {
  20. Console.WriteLine("大家好,我叫{0},我今年{1}岁了,我的性别是{2}",this.Name,this.Age,this.Sex);
  21. }
  22. }

然后写动物的特性类AnimalAttribute类继承于Attribute(特性)

  1. class AnimalAttribute:Attribute
  2. {
  3. //字段和属性描述是否是灵长类
  4. private bool isPrimate;
  5. public bool IsPrimate
  6. {
  7. set { isPrimate = value; }
  8. get { return isPrimate; }
  9. }
  10. }

然后对人类进行动物类描述。即在人类的定义前面加:
[Animal(IsPrimate=true)]//为人类加特性,指定人类是灵长类。

下面我们就可以通过代码来获得人类的特性:

  1. //声明特性对象,并通过Attribute类的静态方法GetCustomAttribute()获得人类的在动物类的特性,并赋值给特性对象
  2. Attribute att1 = Attribute.GetCustomAttribute(typeof(Person), typeof(AnimalAttribute));
  3. //将特性对象转化为动物特性对象
  4. AnimalAttribute animalAtt = att1 as AnimalAttribute;
  5. //检查转化是否成功如果成功则打印这个特性对象的是否是灵长类的属性。
  6. if (animalAtt != null)
  7. {
  8. Console.WriteLine("人类是否是灵长类:{0}",animalAtt.IsPrimate);
  9. }
  10. Console.ReadKey();

第二个例子,类似,给不同的方法添加不同的Animal的类型

  1. // An enumeration of animals. Start at 1 (0 = uninitialized).
  2. public enum Animal
  3. {
  4. // Pets.
  5. Dog = 1,
  6. Cat,
  7. Bird,
  8. }
  9. // A custom attribute to allow a target to have a pet.
  10. public class AnimalTypeAttribute : Attribute
  11. {
  12. // The constructor is called when the attribute is set.
  13. public AnimalTypeAttribute(Animal pet)
  14. {
  15. thePet = pet;
  16. }
  17. // Keep a variable internally ...
  18. protected Animal thePet;
  19. // .. and show a copy to the outside world.
  20. public Animal Pet
  21. {
  22. get { return thePet; }
  23. set { thePet = value; }
  24. }
  25. }
  26. // A test class where each method has its own pet.
  27. class AnimalTypeTestClass
  28. {
  29. [AnimalType(Animal.Dog)]
  30. public void DogMethod() { }
  31. [AnimalType(Animal.Cat)]
  32. public void CatMethod() { }
  33. [AnimalType(Animal.Bird)]
  34. public void BirdMethod() { }
  35. }
  36. class DemoClass
  37. {
  38. static void Main(string[] args)
  39. {
  40. AnimalTypeTestClass testClass = new AnimalTypeTestClass();
  41. Type type = testClass.GetType();
  42. // Iterate through all the methods of the class.
  43. foreach (MethodInfo mInfo in type.GetMethods())
  44. {
  45. // Iterate through all the Attributes for each method.
  46. foreach (Attribute attr in
  47. Attribute.GetCustomAttributes(mInfo))
  48. {
  49. // Check for the AnimalType attribute.
  50. if (attr.GetType() == typeof(AnimalTypeAttribute))
  51. Console.WriteLine(
  52. "Method {0} has a pet {1} attribute.",
  53. mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
  54. }
  55. }
  56. Console.ReadKey();
  57. }
  58. }
  59. /*
  60. * 输出:
  61. * Method DogMethod has a pet Dog attribute.
  62. * Method CatMethod has a pet Cat attribute.
  63. * Method BirdMethod has a pet Bird attribute.
  64. */

发表评论

表情:
评论列表 (有 0 条评论,95人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C# 属性Property

    属性(Property) 是类(class)、结构(structure)和接口(interface)的命名(named)成员。类或结构中的成员变量或方法称为 域(Field)。

    相关 C# 特性Attribute

    特性(Attribute)是用于在运行时传递程序中各种元素(比如类、方法、结构、枚举、组件等)的行为信息的声明性标签。您可以通过使用特性向程序添加声明性信息。一个声明性标签是通