Skip to content

Commit a0e670a

Browse files
Remove uses of SR.GetResourceString throughout the libs (#87912)
Fixes #81338.
1 parent 5ff4fdd commit a0e670a

File tree

19 files changed

+79
-43
lines changed

19 files changed

+79
-43
lines changed

src/libraries/Common/src/System/SR.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ internal static partial class SR
1515
// The trimming tools are also capable of replacing the value of this method when the application is being trimmed.
1616
internal static bool UsingResourceKeys() => s_usingResourceKeys;
1717

18-
internal static string GetResourceString(string resourceKey)
18+
// We can optimize out the resource string blob if we can see all accesses to it happening
19+
// through the generated SR.XXX properties.
20+
// If a call to GetResourceString is left, the optimization gets defeated and we need to keep
21+
// the whole resource blob. It's important to keep this private. CoreCLR's CoreLib gets a free
22+
// pass because the VM needs to be able to call into this, but that's a known set of constants.
23+
#if CORECLR || LEGACY_GETRESOURCESTRING_USER
24+
internal
25+
#else
26+
private
27+
#endif
28+
static string GetResourceString(string resourceKey)
1929
{
2030
if (UsingResourceKeys())
2131
{
@@ -37,7 +47,12 @@ internal static string GetResourceString(string resourceKey)
3747
return resourceString!; // only null if missing resources
3848
}
3949

40-
internal static string GetResourceString(string resourceKey, string defaultString)
50+
#if LEGACY_GETRESOURCESTRING_USER
51+
internal
52+
#else
53+
private
54+
#endif
55+
static string GetResourceString(string resourceKey, string defaultString)
4156
{
4257
string resourceString = GetResourceString(resourceKey);
4358

src/libraries/Microsoft.CSharp/src/Microsoft.CSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<EnableComBinder Condition="'$(TargetPlatformIdentifier)' == 'windows'">true</EnableComBinder>
1010
<DefineConstants Condition="'$(EnableComBinder)' == 'true'">$(DefineConstants);ENABLECOMBINDER</DefineConstants>
1111
<AllowUnsafeBlocks Condition="'$(EnableComBinder)' == 'true'">true</AllowUnsafeBlocks>
12+
<DefineConstants>$(DefineConstants);LEGACY_GETRESOURCESTRING_USER</DefineConstants>
1213
<EnableAOTAnalyzer>false</EnableAOTAnalyzer>
1314
</PropertyGroup>
1415
<ItemGroup>

src/libraries/Microsoft.Extensions.Primitives/src/ThrowHelper.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,20 @@ internal static ArgumentException GetArgumentException(ExceptionResource resourc
5858

5959
private static string GetResourceText(ExceptionResource resource)
6060
{
61-
return SR.GetResourceString(GetResourceName(resource));
61+
Debug.Assert(Enum.IsDefined(typeof(ExceptionResource), resource),
62+
"The enum value is not defined, please check the ExceptionResource Enum.");
63+
64+
switch (resource)
65+
{
66+
case ExceptionResource.Argument_InvalidOffsetLength: return SR.Argument_InvalidOffsetLength;
67+
case ExceptionResource.Argument_InvalidOffsetLengthStringSegment: return SR.Argument_InvalidOffsetLengthStringSegment;
68+
case ExceptionResource.Capacity_CannotChangeAfterWriteStarted: return SR.Capacity_CannotChangeAfterWriteStarted;
69+
case ExceptionResource.Capacity_NotEnough: return SR.Capacity_NotEnough;
70+
case ExceptionResource.Capacity_NotUsedEntirely: return SR.Capacity_NotUsedEntirely;
71+
default:
72+
Debug.Fail($"Unexpected resource {resource}");
73+
return "";
74+
}
6275
}
6376

6477
private static string GetArgumentName(ExceptionArgument argument)
@@ -68,14 +81,6 @@ private static string GetArgumentName(ExceptionArgument argument)
6881

6982
return argument.ToString();
7083
}
71-
72-
private static string GetResourceName(ExceptionResource resource)
73-
{
74-
Debug.Assert(Enum.IsDefined(typeof(ExceptionResource), resource),
75-
"The enum value is not defined, please check the ExceptionResource Enum.");
76-
77-
return resource.ToString();
78-
}
7984
}
8085

8186
internal enum ExceptionArgument

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ArrayConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ArrayConverter : CollectionConverter
1919
{
2020
if (destinationType == typeof(string) && value is Array)
2121
{
22-
return SR.Format(SR.GetResourceString(nameof(SR.Array), "{0} Array"), value.GetType().Name);
22+
return SR.Format(SR.UsingResourceKeys() ? "{0} Array" : SR.Array, value.GetType().Name);
2323
}
2424

2525
return base.ConvertTo(context, culture, value, destinationType);

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/CollectionConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class CollectionConverter : TypeConverter
2020
{
2121
if (destinationType == typeof(string) && value is ICollection)
2222
{
23-
return SR.GetResourceString(nameof(SR.Collection), "(Collection)");
23+
return SR.UsingResourceKeys() ? "(Collection)" : SR.Collection;
2424
}
2525

2626
return base.ConvertTo(context, culture, value, destinationType);

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/CultureInfoConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class CultureInfoConverter : TypeConverter
2222
/// <summary>
2323
/// Retrieves the "default" name for our culture.
2424
/// </summary>
25-
private static string DefaultCultureString => SR.GetResourceString(nameof(SR.CultureInfoConverterDefaultCultureString), "(Default)");
25+
private static string DefaultCultureString => SR.UsingResourceKeys() ? "(Default)" : SR.CultureInfoConverterDefaultCultureString;
2626

2727
private const string DefaultInvariantCultureString = "(Default)";
2828

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/Design/DesignerOptionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContex
461461
{
462462
if (destinationType == typeof(string))
463463
{
464-
return SR.GetResourceString(nameof(SR.CollectionConverterText), "(Collection)");
464+
return SR.UsingResourceKeys() ? "(Collection)" : SR.CollectionConverterText;
465465
}
466466
return base.ConvertTo(cxt, culture, value, destinationType);
467467
}

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ExtendedPropertyDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public override string DisplayName
9999
string? providerName = site?.Name;
100100
if (providerName != null && providerName.Length > 0)
101101
{
102-
name = SR.Format(SR.GetResourceString(nameof(SR.MetaExtenderName), "{0} on {1}"), name, providerName);
102+
name = SR.Format(SR.UsingResourceKeys() ? "{0} on {1}" : SR.MetaExtenderName, name, providerName);
103103
}
104104
}
105105
return name;

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/InstanceCreationEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace System.ComponentModel
1111
/// </summary>
1212
public abstract class InstanceCreationEditor
1313
{
14-
public virtual string Text => SR.GetResourceString(nameof(SR.InstanceCreationEditorDefaultText), "(New...)");
14+
public virtual string Text => SR.UsingResourceKeys() ? "(New...)" : SR.InstanceCreationEditorDefaultText;
1515

1616
/// <summary>
1717
/// This method is invoked when you user chooses the link displayed by the PropertyGrid for the InstanceCreationEditor.

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/MultilineStringConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class MultilineStringConverter : TypeConverter
2020

2121
if (destinationType == typeof(string) && value is string)
2222
{
23-
return SR.GetResourceString(nameof(SR.Text), "(Text)");
23+
return SR.UsingResourceKeys() ? "(Text)" : SR.Text;
2424
}
2525

2626
return base.ConvertTo(context, culture, value, destinationType);

0 commit comments

Comments
 (0)