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
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@
<Compile Include="..\..\src\Resources\ResDescriptionAttribute.cs">
<Link>Resources\ResDescriptionAttribute.cs</Link>
</Compile>
<Compile Include="..\..\src\Resources\StringsHelper.cs" >
<Link>Resources\StringsHelper.Common.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)' == 'netstandard' OR '$(TargetGroup)' == 'netcoreapp' OR '$(IsUAPAssembly)' == 'true'">
<Compile Include="Microsoft.Data.SqlClient.TypeForwards.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Globalization;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Threading;

namespace System
{
internal partial class StringsHelper : Strings
{
private static StringsHelper s_loader = null;
private readonly ResourceManager _resources;

internal StringsHelper()
{
_resources = new ResourceManager("Microsoft.Data.SqlClient.Resources.Strings", GetType().Assembly);
}

private static StringsHelper GetLoader()
{
if (s_loader == null)
{
StringsHelper sr = new();
Interlocked.CompareExchange(ref s_loader, sr, null);
}
return s_loader;
}

// This method is used to decide if we need to append the exception message parameters to the message when calling Strings.Format.
// by default it returns false.
// Native code generators can replace the value this returns based on user input at the time of native code generation.
Expand All @@ -41,42 +20,6 @@ private static bool UsingResourceKeys()
return false;
}

public static string GetResourceString(string res)
{
StringsHelper sys = GetLoader();
if (sys == null)
return null;

// If "res" is a resource id, temp will not be null, "res" will contain the retrieved resource string.
// If "res" is not a resource id, temp will be null.
string temp = sys._resources.GetString(res, Culture);
if (temp != null)
res = temp;

return res;
}

public static string GetString(string res, params object[] args)
{
res = GetResourceString(res);
if (args != null && args.Length > 0)
{
for (int i = 0; i < args.Length; i++)
{
string value = args[i] as string;
if (value != null && value.Length > 1024)
{
args[i] = value.Substring(0, 1024 - 3) + "...";
}
}
return string.Format(CultureInfo.CurrentCulture, res, args);
}
else
{
return res;
}
}

public static string Format(string resourceFormat, params object[] args)
{
if (args != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,9 @@
<DesignTime>True</DesignTime>
<DependentUpon>$(ResxFileName).resx</DependentUpon>
</Compile>
<Compile Include="Resources\$(ResxFileName)Helper.cs" />
<Compile Include="..\..\src\Resources\$(ResxFileName)Helper.cs" >
<Link>Resources\StringsHelper.cs</Link>
</Compile>
<EmbeddedResource Include="Resources\$(ResxFileName).resx">
<CustomToolNamespace>System</CustomToolNamespace>
<Generator>ResXFileCodeGenerator</Generator>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
using System.Resources;
using System.Threading;

namespace Microsoft.Data
namespace
#if NETFRAMEWORK
Microsoft.Data
#else
System
#endif
{
internal partial class StringsHelper : Strings
{
Expand All @@ -16,12 +21,16 @@ internal partial class StringsHelper : Strings

internal StringsHelper()
{
#if NETFRAMEWORK
_resources = new ResourceManager("SqlClient.Resources.Strings", GetType().Assembly);
#else
_resources = new ResourceManager("Microsoft.Data.SqlClient.Resources.Strings", GetType().Assembly);
#endif
}

private static StringsHelper GetLoader()
{
if (s_loader == null)
if (s_loader is null)
{
StringsHelper sr = new();
Interlocked.CompareExchange(ref s_loader, sr, null);
Expand All @@ -32,27 +41,24 @@ private static StringsHelper GetLoader()
public static string GetResourceString(string res)
{
StringsHelper sys = GetLoader();
if (sys == null)
if (sys is null)
return null;

// If "res" is a resource id, temp will not be null, "res" will contain the retrieved resource string.
// If "res" is not a resource id, temp will be null.
string temp = sys._resources.GetString(res, StringsHelper.Culture);
if (temp != null)
res = temp;

return res;
return temp ?? res;
}

public static string GetString(string res, params object[] args)
{
res = GetResourceString(res);
if (args != null && args.Length > 0)
if (args?.Length > 0)
{
for (int i = 0; i < args.Length; i++)
{
string value = args[i] as string;
if (value != null && value.Length > 1024)
if (value?.Length > 1024)
{
args[i] = value.Substring(0, 1024 - 3) + "...";
}
Expand Down