| | | 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 partial struct AssemblyReference |
| | | 10 | | { |
| | | 11 | | private readonly MetadataReader _reader; |
| | | 12 | | |
| | | 13 | | // Workaround: JIT doesn't generate good code for nested structures, so use raw uint. |
| | | 14 | | private readonly uint _treatmentAndRowId; |
| | | 15 | | |
| | 0 | 16 | | private static readonly Version s_version_4_0_0_0 = new Version(4, 0, 0, 0); |
| | | 17 | | |
| | | 18 | | internal AssemblyReference(MetadataReader reader, uint treatmentAndRowId) |
| | 0 | 19 | | { |
| | 0 | 20 | | _reader = reader; |
| | 0 | 21 | | _treatmentAndRowId = treatmentAndRowId; |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | private int RowId |
| | | 25 | | { |
| | 0 | 26 | | get { return (int)(_treatmentAndRowId & TokenTypeIds.RIDMask); } |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | private bool IsVirtual |
| | | 30 | | { |
| | 0 | 31 | | get { return (_treatmentAndRowId & TokenTypeIds.VirtualBit) != 0; } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public Version Version |
| | | 35 | | { |
| | | 36 | | get |
| | 0 | 37 | | { |
| | 0 | 38 | | if (IsVirtual) |
| | 0 | 39 | | { |
| | 0 | 40 | | return GetVirtualVersion(); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | // change mscorlib version: |
| | 0 | 44 | | if (RowId == _reader.WinMDMscorlibRef) |
| | 0 | 45 | | { |
| | 0 | 46 | | return s_version_4_0_0_0; |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | return _reader.AssemblyRefTable.GetVersion(RowId); |
| | 0 | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public AssemblyFlags Flags |
| | | 54 | | { |
| | | 55 | | get |
| | 0 | 56 | | { |
| | 0 | 57 | | if (IsVirtual) |
| | 0 | 58 | | { |
| | 0 | 59 | | return GetVirtualFlags(); |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | return _reader.AssemblyRefTable.GetFlags(RowId); |
| | 0 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public StringHandle Name |
| | | 67 | | { |
| | | 68 | | get |
| | 0 | 69 | | { |
| | 0 | 70 | | if (IsVirtual) |
| | 0 | 71 | | { |
| | 0 | 72 | | return GetVirtualName(); |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | return _reader.AssemblyRefTable.GetName(RowId); |
| | 0 | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public StringHandle Culture |
| | | 80 | | { |
| | | 81 | | get |
| | 0 | 82 | | { |
| | 0 | 83 | | if (IsVirtual) |
| | 0 | 84 | | { |
| | 0 | 85 | | return GetVirtualCulture(); |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | return _reader.AssemblyRefTable.GetCulture(RowId); |
| | 0 | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public BlobHandle PublicKeyOrToken |
| | | 93 | | { |
| | | 94 | | get |
| | 0 | 95 | | { |
| | 0 | 96 | | if (IsVirtual) |
| | 0 | 97 | | { |
| | 0 | 98 | | return GetVirtualPublicKeyOrToken(); |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | return _reader.AssemblyRefTable.GetPublicKeyOrToken(RowId); |
| | 0 | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | public BlobHandle HashValue |
| | | 106 | | { |
| | | 107 | | get |
| | 0 | 108 | | { |
| | 0 | 109 | | if (IsVirtual) |
| | 0 | 110 | | { |
| | 0 | 111 | | return GetVirtualHashValue(); |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | return _reader.AssemblyRefTable.GetHashValue(RowId); |
| | 0 | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | public CustomAttributeHandleCollection GetCustomAttributes() |
| | 0 | 119 | | { |
| | 0 | 120 | | if (IsVirtual) |
| | 0 | 121 | | { |
| | 0 | 122 | | return GetVirtualCustomAttributes(); |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | return new CustomAttributeHandleCollection(_reader, AssemblyReferenceHandle.FromRowId(RowId)); |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | #region Virtual Rows |
| | | 129 | | private static Version GetVirtualVersion() |
| | 0 | 130 | | { |
| | | 131 | | // currently all projected assembly references have version 4.0.0.0 |
| | 0 | 132 | | return s_version_4_0_0_0; |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | private AssemblyFlags GetVirtualFlags() |
| | 0 | 136 | | { |
| | | 137 | | // use flags from mscorlib ref (specifically PublicKey flag): |
| | 0 | 138 | | return _reader.AssemblyRefTable.GetFlags(_reader.WinMDMscorlibRef); |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | private StringHandle GetVirtualName() |
| | 0 | 142 | | { |
| | 0 | 143 | | return StringHandle.FromVirtualIndex(GetVirtualNameIndex((AssemblyReferenceHandle.VirtualIndex)RowId)); |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | private static StringHandle.VirtualIndex GetVirtualNameIndex(AssemblyReferenceHandle.VirtualIndex index) |
| | 0 | 147 | | { |
| | 0 | 148 | | switch (index) |
| | | 149 | | { |
| | | 150 | | case AssemblyReferenceHandle.VirtualIndex.System_ObjectModel: |
| | 0 | 151 | | return StringHandle.VirtualIndex.System_ObjectModel; |
| | | 152 | | |
| | | 153 | | case AssemblyReferenceHandle.VirtualIndex.System_Runtime: |
| | 0 | 154 | | return StringHandle.VirtualIndex.System_Runtime; |
| | | 155 | | |
| | | 156 | | case AssemblyReferenceHandle.VirtualIndex.System_Runtime_InteropServices_WindowsRuntime: |
| | 0 | 157 | | return StringHandle.VirtualIndex.System_Runtime_InteropServices_WindowsRuntime; |
| | | 158 | | |
| | | 159 | | case AssemblyReferenceHandle.VirtualIndex.System_Runtime_WindowsRuntime: |
| | 0 | 160 | | return StringHandle.VirtualIndex.System_Runtime_WindowsRuntime; |
| | | 161 | | |
| | | 162 | | case AssemblyReferenceHandle.VirtualIndex.System_Runtime_WindowsRuntime_UI_Xaml: |
| | 0 | 163 | | return StringHandle.VirtualIndex.System_Runtime_WindowsRuntime_UI_Xaml; |
| | | 164 | | |
| | | 165 | | case AssemblyReferenceHandle.VirtualIndex.System_Numerics_Vectors: |
| | 0 | 166 | | return StringHandle.VirtualIndex.System_Numerics_Vectors; |
| | | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | Debug.Fail("Unexpected virtual index value"); |
| | | 170 | | return 0; |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | private static StringHandle GetVirtualCulture() |
| | 0 | 174 | | { |
| | 0 | 175 | | return default(StringHandle); |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | private BlobHandle GetVirtualPublicKeyOrToken() |
| | 0 | 179 | | { |
| | 0 | 180 | | switch ((AssemblyReferenceHandle.VirtualIndex)RowId) |
| | | 181 | | { |
| | | 182 | | case AssemblyReferenceHandle.VirtualIndex.System_Runtime_WindowsRuntime: |
| | | 183 | | case AssemblyReferenceHandle.VirtualIndex.System_Runtime_WindowsRuntime_UI_Xaml: |
| | | 184 | | // use key or token from mscorlib ref: |
| | 0 | 185 | | return _reader.AssemblyRefTable.GetPublicKeyOrToken(_reader.WinMDMscorlibRef); |
| | | 186 | | |
| | | 187 | | default: |
| | | 188 | | // use contract assembly key or token: |
| | 0 | 189 | | var hasFullKey = (_reader.AssemblyRefTable.GetFlags(_reader.WinMDMscorlibRef) & AssemblyFlags.Public |
| | 0 | 190 | | return BlobHandle.FromVirtualIndex(hasFullKey ? BlobHandle.VirtualIndex.ContractPublicKey : BlobHand |
| | | 191 | | } |
| | 0 | 192 | | } |
| | | 193 | | |
| | | 194 | | private static BlobHandle GetVirtualHashValue() |
| | 0 | 195 | | { |
| | 0 | 196 | | return default(BlobHandle); |
| | 0 | 197 | | } |
| | | 198 | | |
| | | 199 | | private CustomAttributeHandleCollection GetVirtualCustomAttributes() |
| | 0 | 200 | | { |
| | | 201 | | // return custom attributes applied on mscorlib ref |
| | 0 | 202 | | return new CustomAttributeHandleCollection(_reader, AssemblyReferenceHandle.FromRowId(_reader.WinMDMscorlibR |
| | 0 | 203 | | } |
| | | 204 | | #endregion |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | |