Skip to content

AssemblyNameInfo.FullName should contain PublicKey #105649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace System.Reflection
{
internal static class AssemblyNameFormatter
{
public static string ComputeDisplayName(string name, Version? version, string? cultureName, byte[]? pkt, AssemblyNameFlags flags = 0, AssemblyContentType contentType = 0)
public static string ComputeDisplayName(string name, Version? version, string? cultureName, byte[]? pkt, AssemblyNameFlags flags = 0, AssemblyContentType contentType = 0, byte[]? pk = null)
{
const int PUBLIC_KEY_TOKEN_LEN = 8;
Debug.Assert(name.Length != 0);
Expand Down Expand Up @@ -57,19 +57,28 @@ public static string ComputeDisplayName(string name, Version? version, string? c
vsb.AppendQuoted(cultureName);
}

if (pkt != null)
byte[]? keyOrToken = pkt ?? pk;
if (keyOrToken != null)
{
if (pkt.Length > PUBLIC_KEY_TOKEN_LEN)
throw new ArgumentException();
if (pkt != null)
{
if (pkt.Length > PUBLIC_KEY_TOKEN_LEN)
throw new ArgumentException();

vsb.Append(", PublicKeyToken=");
}
else
{
vsb.Append(", PublicKey=");
}

vsb.Append(", PublicKeyToken=");
if (pkt.Length == 0)
if (keyOrToken.Length == 0)
{
vsb.Append("null");
}
else
{
HexConverter.EncodeToUtf16(pkt, vsb.AppendSpan(pkt.Length * 2), HexConverter.Casing.Lower);
HexConverter.EncodeToUtf16(keyOrToken, vsb.AppendSpan(keyOrToken.Length * 2), HexConverter.Casing.Lower);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,20 @@ public string FullName
{
if (_fullName is null)
{
byte[]? publicKeyToken = ((Flags & AssemblyNameFlags.PublicKey) != 0) ? null :
bool isPublicKey = (Flags & AssemblyNameFlags.PublicKey) != 0;

byte[]? publicKeyOrToken =
#if SYSTEM_PRIVATE_CORELIB
PublicKeyOrToken;
#elif NET8_0_OR_GREATER
!PublicKeyOrToken.IsDefault ? Runtime.InteropServices.ImmutableCollectionsMarshal.AsArray(PublicKeyOrToken) : null;
#else
!PublicKeyOrToken.IsDefault ? PublicKeyOrToken.ToArray() : null;
#endif
_fullName = AssemblyNameFormatter.ComputeDisplayName(Name, Version, CultureName, publicKeyToken,
ExtractAssemblyNameFlags(_flags), ExtractAssemblyContentType(_flags));
_fullName = AssemblyNameFormatter.ComputeDisplayName(Name, Version, CultureName,
pkt: isPublicKey ? null : publicKeyOrToken,
ExtractAssemblyNameFlags(_flags), ExtractAssemblyContentType(_flags),
pk: isPublicKey ? publicKeyOrToken : null);
}

return _fullName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ public void WithPublicTokenKey(string fullName)
Roundtrip(assemblyName);
}

[Theory]
[InlineData("System.IO.Pipelines.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb")]
[InlineData("System.IO.Pipelines.Tests, PublicKey=null")]
public void FullNameContainsPublicKey(string withPublicKey)
{
AssemblyNameInfo assemblyNameInfo = AssemblyNameInfo.Parse(withPublicKey.AsSpan());
Assert.Equal(withPublicKey, assemblyNameInfo.FullName);

AssemblyNameInfo roundTrip = AssemblyNameInfo.Parse(assemblyNameInfo.FullName.AsSpan());
Assert.Equal(withPublicKey, roundTrip.FullName);
}

[Fact]
public void NoPublicKeyOrToken()
{
Expand Down
Loading