Skip to content
Closed
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
10 changes: 0 additions & 10 deletions eng/ILLink.Substitutions.Resources.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,5 @@
<!-- System.Resources.UseSystemResourceKeys removes resource strings and instead uses the resource key as the exception message -->
<assembly fullname="{AssemblyName}" feature="System.Resources.UseSystemResourceKeys" featurevalue="true">
<resource name="{StringResourcesName}.resources" action="remove" />
<type fullname="System.SR">
<method signature="System.Boolean UsingResourceKeys()" body="stub" value="true" />
<method signature="System.Boolean GetUsingResourceKeysSwitchValue()" body="stub" value="true" />
</type>
</assembly>
<assembly fullname="{AssemblyName}" feature="System.Resources.UseSystemResourceKeys" featurevalue="false">
<type fullname="System.SR">
<method signature="System.Boolean UsingResourceKeys()" body="stub" value="false" />
<method signature="System.Boolean GetUsingResourceKeysSwitchValue()" body="stub" value="false" />
</type>
</assembly>
</linker>
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,17 @@ internal void AppendToStackTrace(StringBuilder builder)
string s = DeveloperExperience.Default.CreateStackTraceString(_ipAddress, _needFileInfo, out bool isStackTraceHidden);
if (!isStackTraceHidden)
{
// Passing a default string for "at" in case SR.UsingResourceKeys() is true
// 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.UsingResourceKeys() ? "at" : SR.Word_At;
string word_At = SR.UsingResourceKeys ? "at" : SR.Word_At;
builder.Append(" ").Append(word_At).Append(' ');
builder.AppendLine(s);
}
}
if (_isLastFrameFromForeignExceptionStackTrace)
{
// Passing default for Exception_EndStackTraceFromPreviousThrow in case SR.UsingResourceKeys is set.
builder.AppendLine(SR.UsingResourceKeys() ?
builder.AppendLine(SR.UsingResourceKeys ?
"--- End of stack trace from previous location ---" :
SR.Exception_EndStackTraceFromPreviousThrow);
}
Expand Down
30 changes: 16 additions & 14 deletions src/libraries/Common/src/System/SR.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Resources;

namespace System
{
internal static partial class SR
{
private static readonly bool s_usingResourceKeys = GetUsingResourceKeysSwitchValue();
private static readonly bool s_usingResourceKeys = GetUsingResourceKeysSwitchValue;

// This method is a target of ILLink substitution.
private static bool GetUsingResourceKeysSwitchValue() => AppContext.TryGetSwitch("System.Resources.UseSystemResourceKeys", out bool usingResourceKeys) ? usingResourceKeys : false;
[FeatureSwitchDefinition("System.Resources.UseSystemResourceKeys")]
private static bool GetUsingResourceKeysSwitchValue => AppContext.TryGetSwitch("System.Resources.UseSystemResourceKeys", out bool usingResourceKeys) ? usingResourceKeys : false;

// This method is used to decide if we need to append the exception message parameters to the message when calling SR.Format.
// This property is used to decide if we need to append the exception message parameters to the message when calling SR.Format.
// by default it returns the value of System.Resources.UseSystemResourceKeys AppContext switch or false if not specified.
// Native code generators can replace the value this returns based on user input at the time of native code generation.
// 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;
[FeatureSwitchDefinition("System.Resources.UseSystemResourceKeys")]
internal static bool UsingResourceKeys => s_usingResourceKeys;

// We can optimize out the resource string blob if we can see all accesses to it happening
// through the generated SR.XXX properties.
Expand All @@ -30,7 +32,7 @@ internal static partial class SR
#endif
static string GetResourceString(string resourceKey)
{
if (UsingResourceKeys())
if (UsingResourceKeys)
{
return resourceKey;
}
Expand Down Expand Up @@ -64,7 +66,7 @@ static string GetResourceString(string resourceKey, string defaultString)

internal static string Format(string resourceFormat, object? p1)
{
if (UsingResourceKeys())
if (UsingResourceKeys)
{
return string.Join(", ", resourceFormat, p1);
}
Expand All @@ -74,7 +76,7 @@ internal static string Format(string resourceFormat, object? p1)

internal static string Format(string resourceFormat, object? p1, object? p2)
{
if (UsingResourceKeys())
if (UsingResourceKeys)
{
return string.Join(", ", resourceFormat, p1, p2);
}
Expand All @@ -84,7 +86,7 @@ internal static string Format(string resourceFormat, object? p1, object? p2)

internal static string Format(string resourceFormat, object? p1, object? p2, object? p3)
{
if (UsingResourceKeys())
if (UsingResourceKeys)
{
return string.Join(", ", resourceFormat, p1, p2, p3);
}
Expand All @@ -96,7 +98,7 @@ internal static string Format(string resourceFormat, params object?[]? args)
{
if (args != null)
{
if (UsingResourceKeys())
if (UsingResourceKeys)
{
return resourceFormat + ", " + string.Join(", ", args);
}
Expand All @@ -109,7 +111,7 @@ internal static string Format(string resourceFormat, params object?[]? args)

internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1)
{
if (UsingResourceKeys())
if (UsingResourceKeys)
{
return string.Join(", ", resourceFormat, p1);
}
Expand All @@ -119,7 +121,7 @@ internal static string Format(IFormatProvider? provider, string resourceFormat,

internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1, object? p2)
{
if (UsingResourceKeys())
if (UsingResourceKeys)
{
return string.Join(", ", resourceFormat, p1, p2);
}
Expand All @@ -129,7 +131,7 @@ internal static string Format(IFormatProvider? provider, string resourceFormat,

internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1, object? p2, object? p3)
{
if (UsingResourceKeys())
if (UsingResourceKeys)
{
return string.Join(", ", resourceFormat, p1, p2, p3);
}
Expand All @@ -141,7 +143,7 @@ internal static string Format(IFormatProvider? provider, string resourceFormat,
{
if (args != null)
{
if (UsingResourceKeys())
if (UsingResourceKeys)
{
return resourceFormat + ", " + string.Join(", ", args);
}
Expand Down
41 changes: 23 additions & 18 deletions src/libraries/Common/src/System/SR.vb
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,32 @@ Imports System.Resources
Namespace System

Friend NotInheritable Class SR
Private Shared ReadOnly s_usingResourceKeys As Boolean = GetUsingResourceKeysSwitchValue()
Private Shared ReadOnly s_usingResourceKeys As Boolean = GetUsingResourceKeysSwitchValue

<System.Diagnostics.CodeAnalysis.FeatureSwitchDefinitionAttribute("System.Resources.UseSystemResourceKeys")>
Private Shared ReadOnly Property GetUsingResourceKeysSwitchValue() As Boolean
Get
Dim usingResourceKeys As Boolean
If (AppContext.TryGetSwitch("System.Resources.UseSystemResourceKeys", usingResourceKeys)) Then
Return usingResourceKeys
End If

Private Shared Function GetUsingResourceKeysSwitchValue() As Boolean
Dim usingResourceKeys As Boolean
If (AppContext.TryGetSwitch("System.Resources.UseSystemResourceKeys", usingResourceKeys)) Then
Return usingResourceKeys
End If
Return False
End Get
End Property

Return False
End Function

' This method Is used to decide if we need to append the exception message parameters to the message when calling SR.Format.
' This property is used to decide if we need to append the exception message parameters to the message when calling SR.Format.
' by default it returns the value of System.Resources.UseSystemResourceKeys AppContext switch Or false if Not specified.
' Native code generators can replace the value this returns based on user input at the time of native code generation.
' The trimming tools are also capable of replacing the value of this method when the application Is being trimmed.
Public Shared Function UsingResourceKeys() As Boolean
Return s_usingResourceKeys
End Function
Public Shared ReadOnly Property UsingResourceKeys() As Boolean
Get
Return s_usingResourceKeys
End Get
End Property

Friend Shared Function GetResourceString(ByVal resourceKey As String, Optional ByVal defaultString As String = Nothing) As String
If (UsingResourceKeys()) Then
If (UsingResourceKeys) Then
Return If(defaultString, resourceKey)
End If

Expand All @@ -52,7 +57,7 @@ Namespace System

Friend Shared Function Format(ByVal resourceFormat As String, ParamArray args() As Object) As String
If args IsNot Nothing Then
If (UsingResourceKeys()) Then
If (UsingResourceKeys) Then
Return resourceFormat + String.Join(", ", args)
End If
Return String.Format(resourceFormat, args)
Expand All @@ -62,7 +67,7 @@ Namespace System

<Global.System.Runtime.CompilerServices.MethodImpl(Global.System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>
Friend Shared Function Format(ByVal resourceFormat As String, p1 As Object) As String
If (UsingResourceKeys()) Then
If (UsingResourceKeys) Then
Return String.Join(", ", resourceFormat, p1)
End If

Expand All @@ -71,7 +76,7 @@ Namespace System

<Global.System.Runtime.CompilerServices.MethodImpl(Global.System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>
Friend Shared Function Format(ByVal resourceFormat As String, p1 As Object, p2 As Object) As String
If (UsingResourceKeys()) Then
If (UsingResourceKeys) Then
Return String.Join(", ", resourceFormat, p1, p2)
End If

Expand All @@ -80,7 +85,7 @@ Namespace System

<Global.System.Runtime.CompilerServices.MethodImpl(Global.System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>
Friend Shared Function Format(ByVal resourceFormat As String, p1 As Object, p2 As Object, p3 As Object) As String
If (UsingResourceKeys()) Then
If (UsingResourceKeys) Then
Return String.Join(", ", resourceFormat, p1, p2, p3)
End If
Return String.Format(resourceFormat, p1, p2, p3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@
<Compile Include="Specs\Types\SimpleTypeSpec.cs" />
<Compile Include="Specs\Types\TypeSpec.cs" />
</ItemGroup>

<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<Compile Remove="LoggerMessageGenerator.Roslyn4.0.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" Link="Common\System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.cs" Link="Production\ValueListBuilder.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.Pop.cs" Link="Production\ValueListBuilder.Pop.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" Link="Common\System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.cs" Link="Production\ValueListBuilder.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.Pop.cs" Link="Production\ValueListBuilder.Pop.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" Link="Common\System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@
<Compile Include="TypeDeclarationSyntaxReceiver.cs" />
</ItemGroup>

<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" />
</ItemGroup>

</Project>
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.UsingResourceKeys() ? "{0} Array" : SR.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.UsingResourceKeys() ? "(Collection)" : SR.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.UsingResourceKeys() ? "(Default)" : SR.CultureInfoConverterDefaultCultureString;
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.UsingResourceKeys() ? "(Collection)" : SR.CollectionConverterText;
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.UsingResourceKeys() ? "{0} on {1}" : SR.MetaExtenderName, 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.UsingResourceKeys() ? "(New...)" : SR.InstanceCreationEditorDefaultText;
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.UsingResourceKeys() ? "(Text)" : SR.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.UsingResourceKeys() ? "(none)" : SR.toStringNone;
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.UsingResourceKeys() ? "(null)" : SR.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.UsingResourceKeys() ? "(null)" : SR.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.UsingResourceKeys() ? "(none)" : SR.none;
return SR.UsingResourceKeys ? "(none)" : SR.none;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ System.Composition.CompositionContext</PackageDescription>
<Compile Include="System\Composition\Runtime\Util\Formatters.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp' or $([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '9.0'))">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ System.IO.Pipelines.PipeReader</PackageDescription>
<Reference Include="System.Threading.ThreadPool" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp' or $([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '9.0'))">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\FeatureSwitchDefinitionAttribute.cs" />
</ItemGroup>

</Project>
Loading