| | | 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.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | #if !SYSTEM_PRIVATE_CORELIB |
| | | 9 | | using System.Collections.Immutable; |
| | | 10 | | using System.Linq; |
| | | 11 | | #endif |
| | | 12 | | |
| | | 13 | | namespace System.Reflection.Metadata |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Describes an assembly. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// It's a more lightweight, immutable version of <seealso cref="AssemblyName"/> that does not pre-allocate <seealso |
| | | 20 | | /// </remarks> |
| | | 21 | | [DebuggerDisplay("{FullName}")] |
| | | 22 | | #if SYSTEM_REFLECTION_METADATA |
| | | 23 | | public |
| | | 24 | | #else |
| | | 25 | | internal |
| | | 26 | | #endif |
| | | 27 | | sealed class AssemblyNameInfo |
| | | 28 | | { |
| | | 29 | | internal readonly AssemblyNameFlags _flags; |
| | | 30 | | private string? _fullName; |
| | | 31 | | |
| | | 32 | | #if !SYSTEM_PRIVATE_CORELIB |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the AssemblyNameInfo class. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="name">The simple name of the assembly.</param> |
| | | 37 | | /// <param name="version">The version of the assembly.</param> |
| | | 38 | | /// <param name="cultureName">The name of the culture associated with the assembly.</param> |
| | | 39 | | /// <param name="flags">The attributes of the assembly.</param> |
| | | 40 | | /// <param name="publicKeyOrToken">The public key or its token. Set <paramref name="flags"/> to <seealso cref="A |
| | | 41 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception> |
| | 0 | 42 | | public AssemblyNameInfo(string name, Version? version = null, string? cultureName = null, AssemblyNameFlags flag |
| | 0 | 43 | | { |
| | 0 | 44 | | Name = name ?? throw new ArgumentNullException(nameof(name)); |
| | 0 | 45 | | Version = version; |
| | 0 | 46 | | CultureName = cultureName; |
| | 0 | 47 | | _flags = flags; |
| | 0 | 48 | | PublicKeyOrToken = publicKeyOrToken; |
| | 0 | 49 | | } |
| | | 50 | | #endif |
| | | 51 | | |
| | 7156 | 52 | | internal AssemblyNameInfo(AssemblyNameParser.AssemblyNameParts parts) |
| | 7156 | 53 | | { |
| | 7156 | 54 | | Name = parts._name; |
| | 7156 | 55 | | Version = parts._version; |
| | 7156 | 56 | | CultureName = parts._cultureName; |
| | 7156 | 57 | | _flags = parts._flags; |
| | | 58 | | #if SYSTEM_PRIVATE_CORELIB |
| | | 59 | | PublicKeyOrToken = parts._publicKeyOrToken; |
| | | 60 | | #else |
| | 7156 | 61 | | PublicKeyOrToken = parts._publicKeyOrToken is null ? default : parts._publicKeyOrToken.Length == 0 |
| | 7156 | 62 | | ? ImmutableArray<byte>.Empty |
| | 7156 | 63 | | #if NET |
| | 7156 | 64 | | : Runtime.InteropServices.ImmutableCollectionsMarshal.AsImmutableArray(parts._publicKeyOrToken); |
| | | 65 | | #else |
| | | 66 | | : ImmutableArray.Create(parts._publicKeyOrToken); |
| | | 67 | | #endif |
| | | 68 | | #endif |
| | 7156 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the simple name of the assembly. |
| | | 73 | | /// </summary> |
| | 13928 | 74 | | public string Name { get; } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the version of the assembly. |
| | | 78 | | /// </summary> |
| | 13928 | 79 | | public Version? Version { get; } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets the name of the culture associated with the assembly. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <remarks> |
| | | 85 | | /// Do not create a <see cref="System.Globalization.CultureInfo"/> instance from this string unless |
| | | 86 | | /// you know the string has originated from a trustworthy source. |
| | | 87 | | /// </remarks> |
| | 13928 | 88 | | public string? CultureName { get; } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets the attributes of the assembly. |
| | | 92 | | /// </summary> |
| | 10056 | 93 | | public AssemblyNameFlags Flags => _flags; |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Gets the public key or the public key token of the assembly. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <remarks>Check <seealso cref="Flags"/> for <seealso cref="AssemblyNameFlags.PublicKey"/> flag to see whether |
| | | 99 | | #if SYSTEM_PRIVATE_CORELIB |
| | | 100 | | public byte[]? PublicKeyOrToken { get; } |
| | | 101 | | #else |
| | 10056 | 102 | | public ImmutableArray<byte> PublicKeyOrToken { get; } |
| | | 103 | | #endif |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets the full name of the assembly, also known as the display name. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <remarks>In contrary to <seealso cref="AssemblyName.FullName"/> it does not validate public key token neithe |
| | | 109 | | public string FullName |
| | | 110 | | { |
| | | 111 | | get |
| | 3872 | 112 | | { |
| | 3872 | 113 | | if (_fullName is null) |
| | 3872 | 114 | | { |
| | 3872 | 115 | | ValueStringBuilder vsb = new(stackalloc char[256]); |
| | 3872 | 116 | | AppendFullName(ref vsb); |
| | 3872 | 117 | | _fullName = vsb.ToString(); |
| | 3872 | 118 | | } |
| | | 119 | | |
| | 3872 | 120 | | return _fullName; |
| | 3872 | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | internal void AppendFullName(ref ValueStringBuilder vsb) |
| | 10056 | 125 | | { |
| | 10056 | 126 | | if (_fullName is not null) |
| | 0 | 127 | | { |
| | 0 | 128 | | vsb.Append(_fullName); |
| | 0 | 129 | | } |
| | | 130 | | else |
| | 10056 | 131 | | { |
| | 10056 | 132 | | bool isPublicKey = (Flags & AssemblyNameFlags.PublicKey) != 0; |
| | | 133 | | |
| | 10056 | 134 | | byte[]? publicKeyOrToken = |
| | 10056 | 135 | | #if SYSTEM_PRIVATE_CORELIB |
| | 10056 | 136 | | PublicKeyOrToken; |
| | 10056 | 137 | | #elif NET |
| | 10056 | 138 | | !PublicKeyOrToken.IsDefault ? Runtime.InteropServices.ImmutableCollectionsMarshal.AsArray(PublicKeyO |
| | | 139 | | #else |
| | | 140 | | !PublicKeyOrToken.IsDefault ? PublicKeyOrToken.ToArray() : null; |
| | | 141 | | #endif |
| | 10056 | 142 | | AssemblyNameFormatter.AppendDisplayName(ref vsb, Name, Version, CultureName, |
| | 10056 | 143 | | pkt: isPublicKey ? null : publicKeyOrToken, |
| | 10056 | 144 | | ExtractAssemblyNameFlags(_flags), ExtractAssemblyContentType(_flags), |
| | 10056 | 145 | | pk: isPublicKey ? publicKeyOrToken : null); |
| | 10056 | 146 | | } |
| | 10056 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Initializes a new instance of the <seealso cref="AssemblyName"/> class based on the stored information. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <remarks> |
| | | 153 | | /// Do not create an <see cref="AssemblyName"/> instance with <see cref="CultureName"/> string unless |
| | | 154 | | /// you know the string has originated from a trustworthy source. |
| | | 155 | | /// </remarks> |
| | | 156 | | public AssemblyName ToAssemblyName() |
| | 0 | 157 | | { |
| | 0 | 158 | | AssemblyName assemblyName = new(); |
| | 0 | 159 | | assemblyName.Name = Name; |
| | 0 | 160 | | assemblyName.CultureName = CultureName; |
| | 0 | 161 | | assemblyName.Version = Version; |
| | 0 | 162 | | assemblyName.Flags = Flags; |
| | 0 | 163 | | assemblyName.ContentType = ExtractAssemblyContentType(_flags); |
| | | 164 | | #pragma warning disable SYSLIB0037 // Type or member is obsolete |
| | 0 | 165 | | assemblyName.ProcessorArchitecture = ExtractProcessorArchitecture(_flags); |
| | | 166 | | #pragma warning restore SYSLIB0037 // Type or member is obsolete |
| | | 167 | | |
| | | 168 | | #if SYSTEM_PRIVATE_CORELIB |
| | | 169 | | if (PublicKeyOrToken is not null) |
| | | 170 | | { |
| | | 171 | | if ((Flags & AssemblyNameFlags.PublicKey) != 0) |
| | | 172 | | { |
| | | 173 | | assemblyName.SetPublicKey(PublicKeyOrToken); |
| | | 174 | | } |
| | | 175 | | else |
| | | 176 | | { |
| | | 177 | | assemblyName.SetPublicKeyToken(PublicKeyOrToken); |
| | | 178 | | } |
| | | 179 | | } |
| | | 180 | | #else |
| | 0 | 181 | | if (!PublicKeyOrToken.IsDefault) |
| | 0 | 182 | | { |
| | | 183 | | // A copy of the array needs to be created, as AssemblyName allows for the mutation of provided array. |
| | 0 | 184 | | if ((Flags & AssemblyNameFlags.PublicKey) != 0) |
| | 0 | 185 | | { |
| | 0 | 186 | | assemblyName.SetPublicKey(PublicKeyOrToken.ToArray()); |
| | 0 | 187 | | } |
| | | 188 | | else |
| | 0 | 189 | | { |
| | 0 | 190 | | assemblyName.SetPublicKeyToken(PublicKeyOrToken.ToArray()); |
| | 0 | 191 | | } |
| | 0 | 192 | | } |
| | | 193 | | #endif |
| | | 194 | | |
| | 0 | 195 | | return assemblyName; |
| | 0 | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Parses a span of characters into a assembly name. |
| | | 200 | | /// </summary> |
| | | 201 | | /// <param name="assemblyName">A span containing the characters representing the assembly name to parse.</param> |
| | | 202 | | /// <returns>Parsed type name.</returns> |
| | | 203 | | /// <exception cref="ArgumentException">Provided assembly name was invalid.</exception> |
| | | 204 | | public static AssemblyNameInfo Parse(ReadOnlySpan<char> assemblyName) |
| | 0 | 205 | | => TryParse(assemblyName, out AssemblyNameInfo? result) |
| | 0 | 206 | | ? result |
| | 0 | 207 | | : throw new ArgumentException(SR.InvalidAssemblyName, nameof(assemblyName)); |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Tries to parse a span of characters into an assembly name. |
| | | 211 | | /// </summary> |
| | | 212 | | /// <param name="assemblyName">A span containing the characters representing the assembly name to parse.</param> |
| | | 213 | | /// <param name="result">Contains the result when parsing succeeds.</param> |
| | | 214 | | /// <returns>true if assembly name was converted successfully, otherwise, false.</returns> |
| | | 215 | | public static bool TryParse(ReadOnlySpan<char> assemblyName, [NotNullWhen(true)] out AssemblyNameInfo? result) |
| | 11164 | 216 | | { |
| | 11164 | 217 | | AssemblyNameParser.AssemblyNameParts parts = default; |
| | 11164 | 218 | | if (!assemblyName.IsEmpty && AssemblyNameParser.TryParse(assemblyName, ref parts)) |
| | 7156 | 219 | | { |
| | 7156 | 220 | | result = new(parts); |
| | 7156 | 221 | | return true; |
| | | 222 | | } |
| | | 223 | | |
| | 4008 | 224 | | result = null; |
| | 4008 | 225 | | return false; |
| | 11164 | 226 | | } |
| | | 227 | | |
| | | 228 | | internal static AssemblyNameFlags ExtractAssemblyNameFlags(AssemblyNameFlags combinedFlags) |
| | 10056 | 229 | | => combinedFlags & unchecked((AssemblyNameFlags)0xFFFFF10F); |
| | | 230 | | |
| | | 231 | | internal static AssemblyContentType ExtractAssemblyContentType(AssemblyNameFlags flags) |
| | 10056 | 232 | | => (AssemblyContentType)((((int)flags) >> 9) & 0x7); |
| | | 233 | | |
| | | 234 | | internal static ProcessorArchitecture ExtractProcessorArchitecture(AssemblyNameFlags flags) |
| | 0 | 235 | | => (ProcessorArchitecture)((((int)flags) >> 4) & 0x7); |
| | | 236 | | } |
| | | 237 | | } |
| | | 238 | | |