| | | 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 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Reflection.Metadata.Ecma335; |
| | | 6 | | |
| | | 7 | | namespace System.Reflection.Metadata |
| | | 8 | | { |
| | | 9 | | public readonly struct CustomAttribute |
| | | 10 | | { |
| | | 11 | | private readonly MetadataReader _reader; |
| | | 12 | | |
| | | 13 | | // Workaround: JIT doesn't generate good code for nested structures, so use RowId. |
| | | 14 | | private readonly uint _treatmentAndRowId; |
| | | 15 | | |
| | | 16 | | internal CustomAttribute(MetadataReader reader, uint treatmentAndRowId) |
| | 0 | 17 | | { |
| | 0 | 18 | | _reader = reader; |
| | 0 | 19 | | _treatmentAndRowId = treatmentAndRowId; |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | private int RowId |
| | | 23 | | { |
| | 0 | 24 | | get { return (int)(_treatmentAndRowId & TokenTypeIds.RIDMask); } |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | private CustomAttributeHandle Handle |
| | | 28 | | { |
| | 0 | 29 | | get { return CustomAttributeHandle.FromRowId(RowId); } |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | private MethodDefTreatment Treatment |
| | | 33 | | { |
| | 0 | 34 | | get { return (MethodDefTreatment)(_treatmentAndRowId >> TokenTypeIds.RowIdBitCount); } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// The constructor (<see cref="MethodDefinitionHandle"/> or <see cref="MemberReferenceHandle"/>) of the custom |
| | | 39 | | /// </summary> |
| | | 40 | | /// <remarks> |
| | | 41 | | /// Corresponds to Type field of CustomAttribute table in ECMA-335 Standard. |
| | | 42 | | /// </remarks> |
| | | 43 | | public EntityHandle Constructor |
| | | 44 | | { |
| | | 45 | | get |
| | 0 | 46 | | { |
| | 0 | 47 | | return _reader.CustomAttributeTable.GetConstructor(Handle); |
| | 0 | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// The handle of the metadata entity the attribute is applied to. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <remarks> |
| | | 55 | | /// Corresponds to Parent field of CustomAttribute table in ECMA-335 Standard. |
| | | 56 | | /// </remarks> |
| | | 57 | | public EntityHandle Parent |
| | | 58 | | { |
| | | 59 | | get |
| | 0 | 60 | | { |
| | 0 | 61 | | return _reader.CustomAttributeTable.GetParent(Handle); |
| | 0 | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// The value of the attribute. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <remarks> |
| | | 69 | | /// Corresponds to Value field of CustomAttribute table in ECMA-335 Standard. |
| | | 70 | | /// </remarks> |
| | | 71 | | public BlobHandle Value |
| | | 72 | | { |
| | | 73 | | get |
| | 0 | 74 | | { |
| | 0 | 75 | | if (Treatment == 0) |
| | 0 | 76 | | { |
| | 0 | 77 | | return _reader.CustomAttributeTable.GetValue(Handle); |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | return GetProjectedValue(); |
| | 0 | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Decodes the arguments encoded in the value blob. |
| | | 86 | | /// </summary> |
| | | 87 | | public CustomAttributeValue<TType> DecodeValue<TType>(ICustomAttributeTypeProvider<TType> provider) |
| | 0 | 88 | | { |
| | 0 | 89 | | var decoder = new CustomAttributeDecoder<TType>(provider, _reader); |
| | 0 | 90 | | return decoder.DecodeValue(Constructor, Value); |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | #region Projections |
| | | 94 | | |
| | | 95 | | private BlobHandle GetProjectedValue() |
| | 0 | 96 | | { |
| | | 97 | | // The usual pattern for accessing custom attributes differs from pattern for accessing e.g. TypeDef row fie |
| | | 98 | | // The value blob is only accessed when the consumer is about to decode it (which is a nontrivial process), |
| | | 99 | | // while the Constructor and Parent fields are often accessed when searching for a particular attribute. |
| | | 100 | | // |
| | | 101 | | // The current WinMD projections only affect the blob and not the Constructor and Parent values. |
| | | 102 | | // It is thus more efficient to calculate the treatment here (and make GetValue more expensive) and |
| | | 103 | | // avoid calculating the treatment when the CustomAttributeHandle is looked up and CustomAttribute struct |
| | | 104 | | // is initialized. |
| | | 105 | | |
| | 0 | 106 | | CustomAttributeValueTreatment treatment = _reader.CalculateCustomAttributeValueTreatment(Handle); |
| | 0 | 107 | | if (treatment == 0) |
| | 0 | 108 | | { |
| | 0 | 109 | | return _reader.CustomAttributeTable.GetValue(Handle); |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | return GetProjectedValue(treatment); |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | private BlobHandle GetProjectedValue(CustomAttributeValueTreatment treatment) |
| | 0 | 116 | | { |
| | | 117 | | BlobHandle.VirtualIndex virtualIndex; |
| | | 118 | | bool isVersionOrDeprecated; |
| | 0 | 119 | | switch (treatment) |
| | | 120 | | { |
| | | 121 | | case CustomAttributeValueTreatment.AttributeUsageVersionAttribute: |
| | | 122 | | case CustomAttributeValueTreatment.AttributeUsageDeprecatedAttribute: |
| | 0 | 123 | | virtualIndex = BlobHandle.VirtualIndex.AttributeUsage_AllowMultiple; |
| | 0 | 124 | | isVersionOrDeprecated = true; |
| | 0 | 125 | | break; |
| | | 126 | | |
| | | 127 | | case CustomAttributeValueTreatment.AttributeUsageAllowMultiple: |
| | 0 | 128 | | virtualIndex = BlobHandle.VirtualIndex.AttributeUsage_AllowMultiple; |
| | 0 | 129 | | isVersionOrDeprecated = false; |
| | 0 | 130 | | break; |
| | | 131 | | |
| | | 132 | | case CustomAttributeValueTreatment.AttributeUsageAllowSingle: |
| | 0 | 133 | | virtualIndex = BlobHandle.VirtualIndex.AttributeUsage_AllowSingle; |
| | 0 | 134 | | isVersionOrDeprecated = false; |
| | 0 | 135 | | break; |
| | | 136 | | |
| | | 137 | | default: |
| | 0 | 138 | | Debug.Fail($"Unexpected treatment {treatment}"); |
| | | 139 | | return default(BlobHandle); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | // Raw blob format: |
| | | 143 | | // 01 00 - Fixed prolog for CA's |
| | | 144 | | // xx xx xx xx - The Windows.Foundation.Metadata.AttributeTarget value |
| | | 145 | | // 00 00 - Indicates 0 name/value pairs following. |
| | 0 | 146 | | var rawBlob = _reader.CustomAttributeTable.GetValue(Handle); |
| | 0 | 147 | | var rawBlobReader = _reader.GetBlobReader(rawBlob); |
| | 0 | 148 | | if (rawBlobReader.Length != 8) |
| | 0 | 149 | | { |
| | 0 | 150 | | return rawBlob; |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | if (rawBlobReader.ReadInt16() != 1) |
| | 0 | 154 | | { |
| | 0 | 155 | | return rawBlob; |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | AttributeTargets projectedValue = ProjectAttributeTargetValue(rawBlobReader.ReadUInt32()); |
| | 0 | 159 | | if (isVersionOrDeprecated) |
| | 0 | 160 | | { |
| | 0 | 161 | | projectedValue |= AttributeTargets.Constructor | AttributeTargets.Property; |
| | 0 | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | return BlobHandle.FromVirtualIndex(virtualIndex, (ushort)projectedValue); |
| | 0 | 165 | | } |
| | | 166 | | |
| | | 167 | | private static AttributeTargets ProjectAttributeTargetValue(uint rawValue) |
| | 0 | 168 | | { |
| | | 169 | | // Windows.Foundation.Metadata.AttributeTargets.All |
| | 0 | 170 | | if (rawValue == 0xffffffff) |
| | 0 | 171 | | { |
| | 0 | 172 | | return AttributeTargets.All; |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | AttributeTargets result = 0; |
| | | 176 | | |
| | 0 | 177 | | if ((rawValue & 0x00000001) != 0) result |= AttributeTargets.Delegate; |
| | 0 | 178 | | if ((rawValue & 0x00000002) != 0) result |= AttributeTargets.Enum; |
| | 0 | 179 | | if ((rawValue & 0x00000004) != 0) result |= AttributeTargets.Event; |
| | 0 | 180 | | if ((rawValue & 0x00000008) != 0) result |= AttributeTargets.Field; |
| | 0 | 181 | | if ((rawValue & 0x00000010) != 0) result |= AttributeTargets.Interface; |
| | | 182 | | // InterfaceGroup (no equivalent in CLR) |
| | 0 | 183 | | if ((rawValue & 0x00000040) != 0) result |= AttributeTargets.Method; |
| | 0 | 184 | | if ((rawValue & 0x00000080) != 0) result |= AttributeTargets.Parameter; |
| | 0 | 185 | | if ((rawValue & 0x00000100) != 0) result |= AttributeTargets.Property; |
| | 0 | 186 | | if ((rawValue & 0x00000200) != 0) result |= AttributeTargets.Class; |
| | 0 | 187 | | if ((rawValue & 0x00000400) != 0) result |= AttributeTargets.Struct; |
| | | 188 | | // InterfaceImpl (no equivalent in CLR) |
| | | 189 | | |
| | 0 | 190 | | return result; |
| | 0 | 191 | | } |
| | | 192 | | #endregion |
| | | 193 | | } |
| | | 194 | | } |
| | | 195 | | |