Skip to content

Commit

Permalink
Fix the TypeConverter when enabling UseSystemResourceKeys (#68687)
Browse files Browse the repository at this point in the history
* Fix TypeConverter when enabling UseSystemResourceKeys

* Address the feedback

* Keep original behavior and enable running the added test with UseSystemResourceKeys on
  • Loading branch information
tarekgh authored Apr 30, 2022
1 parent a889557 commit 5c2a738
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 18 deletions.
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.Array, value.GetType().Name);
return string.Format(SR.GetResourceString(nameof(SR.Array), "{0} 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.Collection;
return SR.GetResourceString(nameof(SR.Collection), "(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.CultureInfoConverterDefaultCultureString;
private static string DefaultCultureString => SR.GetResourceString(nameof(SR.CultureInfoConverterDefaultCultureString), "(Default)");

private const string DefaultInvariantCultureString = "(Default)";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContex
{
if (destinationType == typeof(string))
{
return SR.CollectionConverterText;
return SR.GetResourceString(nameof(SR.CollectionConverterText), "(Collection)");
}
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.MetaExtenderName, name, providerName);
name = string.Format(SR.GetResourceString(nameof(SR.MetaExtenderName), "{0} on {1}"), 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.InstanceCreationEditorDefaultText;
public virtual string Text => SR.GetResourceString(nameof(SR.InstanceCreationEditorDefaultText), "(New...)");

/// <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.Text;
return SR.GetResourceString(nameof(SR.Text), "(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.toStringNone;
private static readonly string s_none = SR.GetResourceString(nameof(SR.toStringNone), "(none)");
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.Null : value.GetType().FullName;
string? valueTypeName = value == null ? SR.GetResourceString(nameof(SR.Null), "(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.Null : value.GetType().FullName;
string? valueTypeName = value == null ? SR.GetResourceString(nameof(SR.Null), "(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.none;
return SR.GetResourceString(nameof(SR.none), "(none)");
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public override string Description
if (!_replaced)
{
_replaced = true;
DescriptionValue = SR.Format(base.Description);

// We call string.Format here only to keep the original behavior which throws when having null description.
// That will keep the exception is thrown from same original place with the exact parameters.
DescriptionValue = string.Format(base.Description);
}
return base.Description;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public void Add2_Name_Duplicate()
Assert.Equal(typeof(ArgumentException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.True(ex.Message.IndexOf("'dup'") != -1);
Assert.True(ex.Message.IndexOf("'dup'") != -1 || ex.Message.IndexOf(" dup") != -1);
Assert.Null(ex.ParamName);
Assert.Equal(1, container.Components.Count);

Expand All @@ -334,7 +334,7 @@ public void Add2_Name_Duplicate()
Assert.Equal(typeof(ArgumentException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.True(ex.Message.IndexOf("'duP'") != -1);
Assert.True(ex.Message.IndexOf("'duP'") != -1 || ex.Message.IndexOf(" duP") != -1);
Assert.Null(ex.ParamName);
Assert.Equal(1, container.Components.Count);

Expand All @@ -356,7 +356,7 @@ public void Add2_Name_Duplicate()
Assert.Equal(typeof(ArgumentException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.True(ex.Message.IndexOf("'dup'") != -1);
Assert.True(ex.Message.IndexOf("'dup'") != -1 || ex.Message.IndexOf(" dup") != -1);
Assert.Null(ex.ParamName);
Assert.Equal(2, container.Components.Count);
Assert.Equal(1, container2.Components.Count);
Expand Down Expand Up @@ -729,7 +729,7 @@ public void ValidateName_Name_Duplicate()
Assert.Equal(typeof(ArgumentException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.True(ex.Message.IndexOf("'dup'") != -1);
Assert.True(ex.Message.IndexOf("'dup'") != -1 || ex.Message.IndexOf(" dup") != -1);
Assert.Null(ex.ParamName);
Assert.Equal(2, _container.Components.Count);
_container.InvokeValidateName(compB, "whatever");
Expand All @@ -742,7 +742,7 @@ public void ValidateName_Name_Duplicate()
Assert.Equal(typeof(ArgumentException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.True(ex.Message.IndexOf("'dup'") != -1);
Assert.True(ex.Message.IndexOf("'dup'") != -1 || ex.Message.IndexOf(" dup") != -1);
Assert.Null(ex.ParamName);
Assert.Equal(2, _container.Components.Count);
_container.InvokeValidateName(compC, "whatever");
Expand All @@ -757,7 +757,7 @@ public void ValidateName_Name_Duplicate()
Assert.Equal(typeof(ArgumentException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.True(ex.Message.IndexOf("'dup'") != -1);
Assert.True(ex.Message.IndexOf("'dup'") != -1 || ex.Message.IndexOf(" dup") != -1);
Assert.Null(ex.ParamName);
Assert.Equal(2, _container.Components.Count);
_container.InvokeValidateName(compD, "whatever");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.RemoteExecutor;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Tests;
using Xunit;

namespace System.ComponentModel.Tests
Expand Down Expand Up @@ -152,6 +154,23 @@ public void GetCultureName_Overriden_ConversionsReturnsExpected()
Assert.Equal("Fixed", converter.ConvertTo(new CultureInfo("en-US"), typeof(string)));
}

[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[InlineData(false)]
[InlineData(true)]
public void CultureInfoConverterForDefaultValue(bool useSystemResourceKeys)
{
RemoteInvokeOptions options = new RemoteInvokeOptions();
options.RuntimeConfigurationOptions.Add("System.Resources.UseSystemResourceKeys", useSystemResourceKeys);

RemoteExecutor.Invoke(() =>
{
using (new ThreadCultureChange(null, CultureInfo.InvariantCulture))
{
Assert.Equal("", ((CultureInfo)TypeDescriptor.GetConverter(typeof(System.Globalization.CultureInfo)).ConvertFrom(null, null, "(Default)")).Name);
}
}, options).Dispose();
}

private class SubCultureInfoConverter : CultureInfoConverter
{
public new string GetCultureName(CultureInfo culture)
Expand Down

0 comments on commit 5c2a738

Please sign in to comment.