Skip to content

Commit 3710f1a

Browse files
committed
ReadOnlySpan<char> adjustments, enable nullable context, unescape properly
1 parent 41644b2 commit 3710f1a

File tree

6 files changed

+50
-27
lines changed

6 files changed

+50
-27
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Resources/ResourceManagerWrapper.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,10 @@ private ResourceSet ResourceSet
236236
{
237237
if (_resourceSet == null)
238238
{
239-
string manifestResourceName;
239+
//"$(AssemblyShortname).unlocalizable.g"
240+
string manifestResourceName = $"{ReflectionUtils.GetAssemblyPartialName(_assembly)}{UnLocalizableResourceNameSuffix}";
241+
ResourceManager manager = new(manifestResourceName, _assembly);
240242

241-
manifestResourceName = ReflectionUtils.GetAssemblyPartialName(_assembly) + UnLocalizableResourceNameSuffix;
242-
243-
ResourceManager manager = new ResourceManager(manifestResourceName, this._assembly);
244243
_resourceSet = manager.GetResourceSet(CultureInfo.InvariantCulture, true, false);
245244
}
246245

@@ -258,11 +257,9 @@ private ResourceManager ResourceManager
258257
{
259258
if (_resourceManager == null)
260259
{
261-
string baseResourceName; // Our build system always generate a resource base name "$(AssemblyShortname).g"
262-
263-
baseResourceName = ReflectionUtils.GetAssemblyPartialName(_assembly) + LocalizableResourceNameSuffix;
264-
265-
_resourceManager = new ResourceManager(baseResourceName, this._assembly);
260+
// Our build system always generate a resource base name "$(AssemblyShortname).g"
261+
string baseResourceName = $"{ReflectionUtils.GetAssemblyPartialName(_assembly)}{LocalizableResourceNameSuffix}";
262+
_resourceManager = new ResourceManager(baseResourceName, _assembly);
266263
}
267264

268265
return _resourceManager;

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndSourceParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ internal static bool PlatformSupportsTransparentChildWindows
424424
/// <remarks>Not intended to be tested outside test code</remarks>
425425
internal static void SetPlatformSupportsTransparentChildWindowsForTestingOnly(bool value)
426426
{
427-
if (string.Equals(ReflectionUtils.GetAssemblyPartialName(Assembly.GetEntryAssembly()), "drthwndsource", StringComparison.CurrentCultureIgnoreCase))
427+
if (ReflectionUtils.GetAssemblyPartialName(Assembly.GetEntryAssembly()).Equals("drthwndsource", StringComparison.CurrentCultureIgnoreCase))
428428
{
429429
_platformSupportsTransparentChildWindows = value;
430430
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Navigation/BaseUriHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ static internal bool IsComponentEntryAssembly(string component)
347347

348348
if (assembly != null)
349349
{
350-
return (string.Equals(ReflectionUtils.GetAssemblyPartialName(assembly), assemblyName, StringComparison.OrdinalIgnoreCase));
350+
return ReflectionUtils.GetAssemblyPartialName(assembly).Equals(assemblyName, StringComparison.OrdinalIgnoreCase);
351351
}
352352
else
353353
{

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentsTrace.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ static DocumentsTrace()
120120
public DocumentsTrace(string switchName)
121121
{
122122
#if DEBUG
123-
string name = ReflectionUtils.GetAssemblyPartialName(Assembly.GetCallingAssembly());
124-
_switch = new BooleanSwitch(switchName, $"[{name}]");
123+
ReadOnlySpan<char> shortAssemblyName = ReflectionUtils.GetAssemblyPartialName(Assembly.GetCallingAssembly());
124+
_switch = new BooleanSwitch(switchName, $"[{shortAssemblyName}]");
125125
#endif
126126
}
127127

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemResources.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ internal ResourceDictionaries(Assembly assembly)
580580
}
581581
else
582582
{
583-
_assemblyName = ReflectionUtils.GetAssemblyPartialName(assembly);
583+
_assemblyName = ReflectionUtils.GetAssemblyPartialName(assembly).ToString();
584584
}
585585
}
586586

src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/ReflectionUtils.cs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
7+
using System.Runtime.CompilerServices;
58
using System.Reflection;
9+
using System.Text;
610
using System;
711

812
namespace MS.Internal
@@ -12,6 +16,23 @@ namespace MS.Internal
1216
/// </summary>
1317
internal static class ReflectionUtils
1418
{
19+
20+
#if !NETFX
21+
/// <summary>
22+
/// Retrieves the full assembly name by combining the <paramref name="partialName"/> passed in
23+
/// with everything else from <paramref name="assembly"/>.
24+
/// </summary>
25+
internal static string GetFullAssemblyNameFromPartialName(Assembly assembly, string partialName)
26+
{
27+
ArgumentNullException.ThrowIfNull(assembly, nameof(assembly));
28+
string? fullName = assembly.FullName;
29+
ArgumentNullException.ThrowIfNull(fullName, nameof(Assembly.FullName));
30+
31+
AssemblyName name = new(fullName) { Name = partialName };
32+
return name.FullName;
33+
}
34+
#endif
35+
1536
/// <summary>
1637
/// Given an <paramref name="assembly"/>, returns the partial/simple name of the assembly.
1738
/// </summary>
@@ -22,40 +43,45 @@ internal static string GetAssemblyPartialName(Assembly assembly)
2243
#endif
2344
{
2445
#if !NETFX
46+
ArgumentNullException.ThrowIfNull(assembly, nameof(assembly));
2547
// We know that the input is trusted (it will be properly escaped, with ", " between tokens etc.)
2648
// So we can allow ourselves to do a little trick, where we just find the first separator
2749
// You cannot load an assembly (or define) where name is empty, it needs to be at least 1 character
2850
// But we will keep this for consistency of the previous function, maybe I've missed a class
2951
ReadOnlySpan<char> fullName = assembly.FullName;
3052
if (fullName.IsEmpty)
31-
return string.Empty;
53+
return ReadOnlySpan<char>.Empty;
3254

3355
ReadOnlySpan<char> nameSlice = fullName;
34-
// Skip any escaped commas in the name
56+
// Skip any escaped commas in the name if present
3557
int escapedComma = fullName.LastIndexOf("\\,");
3658
if (escapedComma != -1)
3759
nameSlice = nameSlice.Slice(escapedComma + 2);
3860

39-
// Find the start of the next token (usually "Version")
61+
// Find the real ending of the name section
4062
int commaIndex = nameSlice.IndexOf(',');
4163
if (commaIndex != -1)
4264
fullName = fullName.Slice(0, fullName.Length - nameSlice.Length + commaIndex);
4365

66+
// Check if we need to unescape, this is very rare case so we can just do it the dirty way
67+
if (escapedComma != -1 || fullName.Contains('\\'))
68+
UnescapeDirty(ref fullName);
69+
70+
// Since having "," or "=" in the assembly name is very rare, we don't want to inline
71+
[MethodImpl(MethodImplOptions.NoInlining)]
72+
static void UnescapeDirty(ref ReadOnlySpan<char> dirtyName)
73+
{
74+
StringBuilder sb = new(dirtyName.Length);
75+
sb.Append(dirtyName);
76+
sb.Replace("\\", null);
77+
dirtyName = sb.ToString();
78+
}
79+
4480
return fullName;
4581
#else
4682
AssemblyName name = new(assembly.FullName);
4783
return name.Name ?? string.Empty;
4884
#endif
4985
}
50-
51-
/// <summary>
52-
/// Retrieves the full assembly name by combining the <paramref name="partialName"/> passed in
53-
/// with everything else from <paramref name="assembly"/>.
54-
/// </summary>
55-
internal static string GetFullAssemblyNameFromPartialName(Assembly assembly, string partialName)
56-
{
57-
AssemblyName name = new(assembly.FullName) { Name = partialName };
58-
return name.FullName;
59-
}
6086
}
6187
}

0 commit comments

Comments
 (0)