1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Security.Permissions; 6 7 namespace System.ComponentModel 8 { 9 /// <summary> 10 /// <para>Marks instances of objects that are inherited from their base class. This 11 /// class cannot be inherited.</para> 12 /// </summary> 13 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event)] 14 public sealed class InheritanceAttribute : Attribute 15 { 16 /// <summary> 17 /// <para> 18 /// Specifies that the component is inherited. This field is 19 /// read-only. 20 /// </para> 21 /// </summary> 22 public static readonly InheritanceAttribute Inherited = new InheritanceAttribute(InheritanceLevel.Inherited); 23 24 /// <summary> 25 /// <para> 26 /// Specifies that 27 /// the component is inherited and is read-only. This field is 28 /// read-only. 29 /// </para> 30 /// </summary> 31 public static readonly InheritanceAttribute InheritedReadOnly = new InheritanceAttribute(InheritanceLevel.InheritedReadOnly); 32 33 /// <summary> 34 /// <para> 35 /// Specifies that the component is not inherited. This field is 36 /// read-only. 37 /// </para> 38 /// </summary> 39 public static readonly InheritanceAttribute NotInherited = new InheritanceAttribute(InheritanceLevel.NotInherited); 40 41 /// <summary> 42 /// <para> 43 /// Specifies the default value for 44 /// the InheritanceAttribute as NotInherited. 45 /// </para> 46 /// </summary> 47 public static readonly InheritanceAttribute Default = NotInherited; 48 49 /// <summary> 50 /// <para>Initializes a new instance of the System.ComponentModel.Design.InheritanceAttribute 51 /// class.</para> 52 /// </summary> InheritanceAttribute()53 public InheritanceAttribute() 54 { 55 InheritanceLevel = Default.InheritanceLevel; 56 } 57 58 /// <summary> 59 /// <para>Initializes a new instance of the System.ComponentModel.Design.InheritanceAttribute class 60 /// with the specified inheritance 61 /// level.</para> 62 /// </summary> InheritanceAttribute(InheritanceLevel inheritanceLevel)63 public InheritanceAttribute(InheritanceLevel inheritanceLevel) 64 { 65 InheritanceLevel = inheritanceLevel; 66 } 67 68 /// <summary> 69 /// <para> 70 /// Gets or sets 71 /// the current inheritance level stored in this attribute. 72 /// </para> 73 /// </summary> 74 public InheritanceLevel InheritanceLevel { get; } 75 76 /// <summary> 77 /// <para> 78 /// Override to test for equality. 79 /// </para> 80 /// </summary> Equals(object value)81 public override bool Equals(object value) 82 { 83 if (value == this) 84 { 85 return true; 86 } 87 88 if (!(value is InheritanceAttribute)) 89 { 90 return false; 91 } 92 93 InheritanceLevel valueLevel = ((InheritanceAttribute)value).InheritanceLevel; 94 return (valueLevel == InheritanceLevel); 95 } 96 97 /// <summary> 98 /// <para> 99 /// Returns the hashcode for this object. 100 /// </para> 101 /// </summary> GetHashCode()102 public override int GetHashCode() 103 { 104 return base.GetHashCode(); 105 } 106 107 /// <summary> 108 /// <para> 109 /// Gets whether this attribute is the default. 110 /// </para> 111 /// </summary> IsDefaultAttribute()112 public override bool IsDefaultAttribute() 113 { 114 return (Equals(Default)); 115 } 116 117 /// <summary> 118 /// <para> 119 /// Converts this attribute to a string. 120 /// </para> 121 /// </summary> ToString()122 public override string ToString() 123 { 124 return TypeDescriptor.GetConverter(typeof(InheritanceLevel)).ConvertToString(InheritanceLevel); 125 } 126 } 127 } 128 129