-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathSR.cs
More file actions
155 lines (128 loc) · 5.31 KB
/
Copy pathSR.cs
File metadata and controls
155 lines (128 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Resources;
namespace System
{
internal static partial class SR
{
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;
// This method 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;
// 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())
{
return resourceKey;
}
string? resourceString = null;
try
{
resourceString =
#if SYSTEM_PRIVATE_CORELIB || NATIVEAOT
InternalGetResourceString(resourceKey);
#else
ResourceManager.GetString(resourceKey);
#endif
}
catch (MissingManifestResourceException) { }
return resourceString!; // only null if missing resources
}
#if LEGACY_GETRESOURCESTRING_USER
internal
#else
private
#endif
static string GetResourceString(string resourceKey, string defaultString)
{
string resourceString = GetResourceString(resourceKey);
return resourceKey == resourceString || resourceString == null ? defaultString : resourceString;
}
internal static string Format(string resourceFormat, object? p1)
{
if (UsingResourceKeys())
{
return string.Join(", ", resourceFormat, p1);
}
return string.Format(resourceFormat, p1);
}
internal static string Format(string resourceFormat, object? p1, object? p2)
{
if (UsingResourceKeys())
{
return string.Join(", ", resourceFormat, p1, p2);
}
return string.Format(resourceFormat, p1, p2);
}
internal static string Format(string resourceFormat, object? p1, object? p2, object? p3)
{
if (UsingResourceKeys())
{
return string.Join(", ", resourceFormat, p1, p2, p3);
}
return string.Format(resourceFormat, p1, p2, p3);
}
internal static string Format(string resourceFormat, params object?[]? args)
{
if (args != null)
{
if (UsingResourceKeys())
{
return resourceFormat + ", " + string.Join(", ", args);
}
return string.Format(resourceFormat, args);
}
return resourceFormat;
}
internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1)
{
if (UsingResourceKeys())
{
return string.Join(", ", resourceFormat, p1);
}
return string.Format(provider, resourceFormat, p1);
}
internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1, object? p2)
{
if (UsingResourceKeys())
{
return string.Join(", ", resourceFormat, p1, p2);
}
return string.Format(provider, resourceFormat, p1, p2);
}
internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1, object? p2, object? p3)
{
if (UsingResourceKeys())
{
return string.Join(", ", resourceFormat, p1, p2, p3);
}
return string.Format(provider, resourceFormat, p1, p2, p3);
}
internal static string Format(IFormatProvider? provider, string resourceFormat, params object?[]? args)
{
if (args != null)
{
if (UsingResourceKeys())
{
return resourceFormat + ", " + string.Join(", ", args);
}
return string.Format(provider, resourceFormat, args);
}
return resourceFormat;
}
}
}