Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 17 additions & 2 deletions src/libraries/Common/src/System/SR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ internal static partial class SR
// The trimming tools are also capable of replacing the value of this method when the application is being trimmed.
internal static bool UsingResourceKeys() => s_usingResourceKeys;

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

internal static string GetResourceString(string resourceKey, string defaultString)
#if LEGACY_GETRESOURCESTRING_USER
internal
#else
private
#endif
static string GetResourceString(string resourceKey, string defaultString)
{
string resourceString = GetResourceString(resourceKey);

Expand Down
1 change: 1 addition & 0 deletions src/libraries/Microsoft.CSharp/src/Microsoft.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<EnableComBinder Condition="'$(TargetPlatformIdentifier)' == 'windows'">true</EnableComBinder>
<DefineConstants Condition="'$(EnableComBinder)' == 'true'">$(DefineConstants);ENABLECOMBINDER</DefineConstants>
<AllowUnsafeBlocks Condition="'$(EnableComBinder)' == 'true'">true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);LEGACY_GETRESOURCESTRING_USER</DefineConstants>
<EnableAOTAnalyzer>false</EnableAOTAnalyzer>
</PropertyGroup>
<ItemGroup>
Expand Down
22 changes: 13 additions & 9 deletions src/libraries/Microsoft.Extensions.Primitives/src/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,19 @@ internal static ArgumentException GetArgumentException(ExceptionResource resourc

private static string GetResourceText(ExceptionResource resource)
{
return SR.GetResourceString(GetResourceName(resource));
Debug.Assert(Enum.IsDefined(typeof(ExceptionResource), resource),
"The enum value is not defined, please check the ExceptionResource Enum.");

#pragma warning disable CS8524 // "switch is not exhaustive". It actually is.
return resource switch
{
ExceptionResource.Argument_InvalidOffsetLength => SR.Argument_InvalidOffsetLength,
ExceptionResource.Argument_InvalidOffsetLengthStringSegment => SR.Argument_InvalidOffsetLengthStringSegment,
ExceptionResource.Capacity_CannotChangeAfterWriteStarted => SR.Capacity_CannotChangeAfterWriteStarted,
ExceptionResource.Capacity_NotEnough => SR.Capacity_NotEnough,
ExceptionResource.Capacity_NotUsedEntirely => SR.Capacity_NotUsedEntirely,
};
#pragma warning restore CS8524
}

private static string GetArgumentName(ExceptionArgument argument)
Expand All @@ -68,14 +80,6 @@ private static string GetArgumentName(ExceptionArgument argument)

return argument.ToString();
}

private static string GetResourceName(ExceptionResource resource)
{
Debug.Assert(Enum.IsDefined(typeof(ExceptionResource), resource),
"The enum value is not defined, please check the ExceptionResource Enum.");

return resource.ToString();
}
}

internal enum ExceptionArgument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ArrayConverter : CollectionConverter
{
if (destinationType == typeof(string) && value is Array)
{
return SR.Format(SR.GetResourceString(nameof(SR.Array), "{0} Array"), value.GetType().Name);
return SR.Format(SR.UsingResourceKeys() ? "{0} Array" : SR.Array, value.GetType().Name);
}

return base.ConvertTo(context, culture, value, destinationType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CollectionConverter : TypeConverter
{
if (destinationType == typeof(string) && value is ICollection)
{
return SR.GetResourceString(nameof(SR.Collection), "(Collection)");
return SR.UsingResourceKeys() ? "(Collection)" : SR.Collection;
}

return base.ConvertTo(context, culture, value, destinationType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CultureInfoConverter : TypeConverter
/// <summary>
/// Retrieves the "default" name for our culture.
/// </summary>
private static string DefaultCultureString => SR.GetResourceString(nameof(SR.CultureInfoConverterDefaultCultureString), "(Default)");
private static string DefaultCultureString => SR.UsingResourceKeys() ? "(Default)" : SR.CultureInfoConverterDefaultCultureString;

private const string DefaultInvariantCultureString = "(Default)";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContex
{
if (destinationType == typeof(string))
{
return SR.GetResourceString(nameof(SR.CollectionConverterText), "(Collection)");
return SR.UsingResourceKeys() ? "(Collection)" : SR.CollectionConverterText;
}
return base.ConvertTo(cxt, culture, value, destinationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public override string DisplayName
string? providerName = site?.Name;
if (providerName != null && providerName.Length > 0)
{
name = SR.Format(SR.GetResourceString(nameof(SR.MetaExtenderName), "{0} on {1}"), name, providerName);
name = SR.Format(SR.UsingResourceKeys() ? "{0} on {1}" : SR.MetaExtenderName, name, providerName);
}
}
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.ComponentModel
/// </summary>
public abstract class InstanceCreationEditor
{
public virtual string Text => SR.GetResourceString(nameof(SR.InstanceCreationEditorDefaultText), "(New...)");
public virtual string Text => SR.UsingResourceKeys() ? "(New...)" : SR.InstanceCreationEditorDefaultText;

/// <summary>
/// This method is invoked when you user chooses the link displayed by the PropertyGrid for the InstanceCreationEditor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MultilineStringConverter : TypeConverter

if (destinationType == typeof(string) && value is string)
{
return SR.GetResourceString(nameof(SR.Text), "(Text)");
return SR.UsingResourceKeys() ? "(Text)" : SR.Text;
}

return base.ConvertTo(context, culture, value, destinationType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace System.ComponentModel
/// </summary>
public class ReferenceConverter : TypeConverter
{
private static readonly string s_none = SR.GetResourceString(nameof(SR.toStringNone), "(none)");
private static readonly string s_none = SR.UsingResourceKeys() ? "(none)" : SR.toStringNone;
private readonly Type _type;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public virtual bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(t
/// </summary>
protected Exception GetConvertFromException(object? value)
{
string? valueTypeName = value == null ? SR.GetResourceString(nameof(SR.Null), "(null)") : value.GetType().FullName;
string? valueTypeName = value == null ? (SR.UsingResourceKeys() ? "(null)" : SR.Null) : value.GetType().FullName;
throw new NotSupportedException(SR.Format(SR.ConvertFromException, GetType().Name, valueTypeName));
}

Expand All @@ -201,7 +201,7 @@ protected Exception GetConvertFromException(object? value)
/// </summary>
protected Exception GetConvertToException(object? value, Type destinationType)
{
string? valueTypeName = value == null ? SR.GetResourceString(nameof(SR.Null), "(null)") : value.GetType().FullName;
string? valueTypeName = value == null ? (SR.UsingResourceKeys() ? "(null)" : SR.Null) : value.GetType().FullName;
throw new NotSupportedException(SR.Format(SR.ConvertToException, GetType().Name, valueTypeName, destinationType.FullName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(
{
if (value == null)
{
return SR.GetResourceString(nameof(SR.none), "(none)");
return SR.UsingResourceKeys() ? "(none)" : SR.none;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Timer(TimeSpan interval) : this(interval.TotalMilliseconds)
/// Gets or sets a value indicating whether the Timer raises the Tick event each time the specified
/// Interval has elapsed, when Enabled is set to true.
/// </summary>
[TimersDescription(nameof(SR.TimerAutoReset), null), DefaultValue(true)]
[TimersDescription(TimersDescriptionStringId.TimerAutoReset), DefaultValue(true)]
public bool AutoReset
{
get => _autoReset;
Expand All @@ -102,7 +102,7 @@ public bool AutoReset
/// is able to raise events at a defined interval.
/// The default value by design is false, don't change it.
/// </summary>
[TimersDescription(nameof(SR.TimerEnabled), null), DefaultValue(false)]
[TimersDescription(TimersDescriptionStringId.TimerEnabled), DefaultValue(false)]
public bool Enabled
{
get => _enabled;
Expand Down Expand Up @@ -160,7 +160,7 @@ private void UpdateTimer()
/// <summary>
/// Gets or sets the interval on which to raise events.
/// </summary>
[TimersDescription(nameof(SR.TimerInterval), null), DefaultValue(100d)]
[TimersDescription(TimersDescriptionStringId.TimerInterval), DefaultValue(100d)]
public double Interval
{
get => _interval;
Expand All @@ -184,7 +184,7 @@ public double Interval
/// Occurs when the <see cref='System.Timers.Timer.Interval'/> has
/// elapsed.
/// </summary>
[TimersDescription(nameof(SR.TimerIntervalElapsed), null)]
[TimersDescription(TimersDescriptionStringId.TimerIntervalElapsed)]
public event ElapsedEventHandler Elapsed
{
add => _onIntervalElapsed += value;
Expand Down Expand Up @@ -212,7 +212,7 @@ public override ISite? Site
/// Gets or sets the object used to marshal event-handler calls that are issued when
/// an interval has elapsed.
/// </summary>
[DefaultValue(null), TimersDescription(nameof(SR.TimerSynchronizingObject), null)]
[DefaultValue(null), TimersDescription(TimersDescriptionStringId.TimerSynchronizingObject)]
public ISynchronizeInvoke? SynchronizingObject
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ public class TimersDescriptionAttribute : DescriptionAttribute
/// </summary>
public TimersDescriptionAttribute(string description) : base(description) { }

/// <summary>
/// Constructs a new localized sys description.
/// </summary>
internal TimersDescriptionAttribute(string description, string? unused) : base(SR.GetResourceString(description))
{
// Needed for overload resolution
Debug.Assert(unused == null);
}
#pragma warning disable CS8524 // "switch is not exhaustive". It actually is.
internal TimersDescriptionAttribute(TimersDescriptionStringId id) : base(
id switch
{
TimersDescriptionStringId.TimerAutoReset => SR.TimerAutoReset,
TimersDescriptionStringId.TimerEnabled => SR.TimerEnabled,
TimersDescriptionStringId.TimerInterval => SR.TimerInterval,
TimersDescriptionStringId.TimerIntervalElapsed => SR.TimerIntervalElapsed,
TimersDescriptionStringId.TimerSynchronizingObject => SR.TimerSynchronizingObject,
}) { }
#pragma warning restore CS8524

/// <summary>
/// Retrieves the description text.
Expand All @@ -49,4 +52,13 @@ public override string Description
}
}
}

internal enum TimersDescriptionStringId
{
TimerAutoReset,
TimerEnabled,
TimerInterval,
TimerIntervalElapsed,
TimerSynchronizingObject
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ internal void ToString(TraceFormat traceFormat, StringBuilder sb)
{
// Passing a default string for "at" in case SR.UsingResourceKeys() is true
// as this is a special case and we don't want to have "Word_At" on stack traces.
string word_At = SR.GetResourceString(nameof(SR.Word_At), defaultString: "at");
string word_At = SR.UsingResourceKeys() ? "at" : SR.Word_At;
// We also want to pass in a default for inFileLineNumber.
string inFileLineNum = SR.GetResourceString(nameof(SR.StackTrace_InFileLineNumber), defaultString: "in {0}:line {1}");
string inFileILOffset = SR.GetResourceString(nameof(SR.StackTrace_InFileILOffset), defaultString: "in {0}:token 0x{1:x}+0x{2:x}");
string inFileLineNum = SR.UsingResourceKeys() ? "in {0}:line {1}" : SR.StackTrace_InFileLineNumber;
string inFileILOffset = SR.UsingResourceKeys() ? "in {0}:token 0x{1:x}+0x{2:x}" : SR.StackTrace_InFileILOffset;
bool fFirstFrame = true;
for (int iFrameIndex = 0; iFrameIndex < _numOfFrames; iFrameIndex++)
{
Expand Down Expand Up @@ -340,8 +340,7 @@ internal void ToString(TraceFormat traceFormat, StringBuilder sb)
{
sb.AppendLine();
// Passing default for Exception_EndStackTraceFromPreviousThrow in case SR.UsingResourceKeys is set.
sb.Append(SR.GetResourceString(nameof(SR.Exception_EndStackTraceFromPreviousThrow),
defaultString: "--- End of stack trace from previous location ---"));
sb.Append(SR.UsingResourceKeys() ? "--- End of stack trace from previous location ---" : SR.Exception_EndStackTraceFromPreviousThrow);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ internal static unsafe EncodingInfo [] GetEncodings(CodePagesEncodingProvider pr
default: codePageName = new string(&codePageIndex.CodePageName); break;
}

string? resourceName = EncodingNLS.GetLocalizedEncodingNameResource(codePageIndex.CodePage);
string? displayName = null;

if (resourceName != null && resourceName.StartsWith("Globalization_cp_", StringComparison.OrdinalIgnoreCase))
if (!SR.UsingResourceKeys())
{
displayName = SR.GetResourceString(resourceName);
displayName = EncodingNLS.GetLocalizedEncodingNameResource(codePageIndex.CodePage);
}

encodingInfoList[i] = new EncodingInfo(provider, codePageIndex.CodePage, codePageName, displayName ?? codePageName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ internal Guid Identifier
// for COM+ interop purposes, but we can't get the guid or the status of the transaction.
if (TxGuid.Equals(Guid.Empty))
{
throw TransactionException.Create(SR.GetResourceString(SR.CannotGetTransactionIdentifier), null);
throw TransactionException.Create(SR.CannotGetTransactionIdentifier, null);
}

return TxGuid;
Expand Down