Skip to content

Commit e59c8ab

Browse files
authored
Merge pull request #11023 from LakshanF/TrimTestWrnFix2
Bubble up formatter trim warnings
2 parents 158f2af + 7ca07fb commit e59c8ab

23 files changed

+104
-4
lines changed

src/System.Windows.Forms/src/Resources/SR.resx

+4-1
Original file line numberDiff line numberDiff line change
@@ -6964,4 +6964,7 @@ Stack trace where the illegal operation occurred was:
69646964
<data name="FolderBrowserDialogMultiSelectDescr" xml:space="preserve">
69656965
<value>Controls whether multiple folders can be selected in the dialog.</value>
69666966
</data>
6967-
</root>
6967+
<data name="BindingNotSupported" xml:space="preserve">
6968+
<value>Binding operations are not supported when application trimming is enabled.</value>
6969+
</data>
6970+
</root>

src/System.Windows.Forms/src/Resources/xlf/SR.cs.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.de.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.es.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.fr.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.it.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.ja.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.ko.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.pl.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.pt-BR.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.ru.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.tr.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hans.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hant.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/src/System/Windows/Forms/Control.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,7 @@ internal BindingContext? BindingContextInternal
11021102
public virtual BindingContext? BindingContext
11031103
{
11041104
get => BindingContextInternal;
1105+
[RequiresUnreferencedCode(IBindableComponent.ComponentModelTrimIncompatibilityMessage)]
11051106
set => BindingContextInternal = value;
11061107
}
11071108

@@ -7052,6 +7053,11 @@ protected virtual void OnBindingContextChanged(EventArgs e)
70527053
{
70537054
if (Properties.ContainsObjectThatIsNotNull(s_bindingsProperty))
70547055
{
7056+
if (!Binding.IsSupported)
7057+
{
7058+
throw new NotSupportedException(SR.BindingNotSupported);
7059+
}
7060+
70557061
UpdateBindings();
70567062
}
70577063

src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListControl/ListControl.cs

+5
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,11 @@ private void SetDataConnection(object? newDataSource, BindingMemberInfo newDispl
680680
CurrencyManager? newDataManager = null;
681681
if (newDataSource is not null && BindingContext is not null && newDataSource != Convert.DBNull)
682682
{
683+
if (!Binding.IsSupported)
684+
{
685+
throw new NotSupportedException(SR.BindingNotSupported);
686+
}
687+
683688
newDataManager = (CurrencyManager)BindingContext[newDataSource, newDisplayMember.BindingPath];
684689
}
685690

src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStrip.cs

+2
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ public override BindingContext? BindingContext
386386
// we don't have a binding context
387387
return null;
388388
}
389+
390+
[RequiresUnreferencedCode(IBindableComponent.ComponentModelTrimIncompatibilityMessage)]
389391
set
390392
{
391393
if (Properties.GetObject(s_propBindingContext) != value)

src/System.Windows.Forms/src/System/Windows/Forms/DataBinding/BindableComponent.cs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public BindingContext? BindingContext
2828
{
2929
get => _bindingContext ??= [];
3030

31+
[RequiresUnreferencedCode(IBindableComponent.ComponentModelTrimIncompatibilityMessage)]
3132
set
3233
{
3334
if (!Equals(_bindingContext, value))

src/System.Windows.Forms/src/System/Windows/Forms/DataBinding/Binding.cs

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ namespace System.Windows.Forms;
1212
[TypeConverter(typeof(ListBindingConverter))]
1313
public partial class Binding
1414
{
15+
[FeatureSwitchDefinition("System.Windows.Forms.Binding.IsSupported")]
16+
#pragma warning disable IDE0075 // Simplify conditional expression - the simpler expression is hard to read
17+
internal static bool IsSupported => AppContext.TryGetSwitch("System.Windows.Forms.Binding.IsSupported", out bool isSupported) ? isSupported : true;
18+
#pragma warning restore IDE0075 //Simplify conditional expression
19+
1520
private BindingManagerBase? _bindingManagerBase;
1621

1722
private readonly BindToObject _bindToObject;

src/System.Windows.Forms/src/System/Windows/Forms/DataBinding/IBindableComponent.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ namespace System.Windows.Forms;
77

88
public interface IBindableComponent : IComponent
99
{
10+
internal const string ComponentModelTrimIncompatibilityMessage = "Binding is not supported with trimming";
1011
ControlBindingsCollection DataBindings { get; }
11-
BindingContext? BindingContext { get; set; }
12+
BindingContext? BindingContext
13+
{
14+
get;
15+
[RequiresUnreferencedCode(ComponentModelTrimIncompatibilityMessage)]
16+
set;
17+
}
1218
}

src/System.Windows.Forms/src/System/Windows/Forms/Internal/Formatter.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ namespace System.Windows.Forms;
99

1010
internal static class Formatter
1111
{
12+
internal const string ComponentModelTrimIncompatibilityMessage = "ComponentModel APIs are not trimming compatible.";
13+
1214
private static readonly Type s_stringType = typeof(string);
1315
private static readonly Type s_booleanType = typeof(bool);
1416
private static readonly Type s_checkStateType = typeof(CheckState);
1517
private static readonly object s_parseMethodNotFound = new();
1618
private static readonly object s_defaultDataSourceNullValue = DBNull.Value;
17-
1819
/// <summary>
1920
/// Converts a binary value into a format suitable for display to the end user.
2021
/// Used when pushing a value from a back-end data source into a data-bound property on a control.
@@ -25,6 +26,7 @@ internal static class Formatter
2526
/// If the caller is expecting a nullable value back, we must also re-wrap the final result
2627
/// inside a nullable value before returning.
2728
/// </summary>
29+
[RequiresUnreferencedCode(ComponentModelTrimIncompatibilityMessage)]
2830
public static object? FormatObject(
2931
object? value,
3032
Type targetType,
@@ -82,6 +84,7 @@ internal static class Formatter
8284
/// - Uses TypeConverters or IConvertible where appropriate
8385
/// - Throws a FormatException is no suitable conversion can be found
8486
/// </summary>
87+
[RequiresUnreferencedCode(ComponentModelTrimIncompatibilityMessage)]
8588
private static object? FormatObjectInternal(
8689
object? value,
8790
Type targetType,
@@ -431,7 +434,7 @@ private static CultureInfo GetFormatterCulture(IFormatProvider? formatInfo)
431434
/// <summary>
432435
/// Converts a value to the specified type using best Parse() method on that type
433436
/// </summary>
434-
public static object? InvokeStringParseMethod(object? value, Type targetType, IFormatProvider? formatInfo)
437+
public static object? InvokeStringParseMethod(object? value, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type targetType, IFormatProvider? formatInfo)
435438
{
436439
try
437440
{

src/System.Windows.Forms/src/System/Windows/Forms/Layout/Containers/ContainerControl.cs

+2
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ public override BindingContext? BindingContext
260260

261261
return bm;
262262
}
263+
264+
[RequiresUnreferencedCode(IBindableComponent.ComponentModelTrimIncompatibilityMessage)]
263265
set => base.BindingContext = value;
264266
}
265267

src/System.Windows.Forms/src/System/Windows/Forms/Layout/Containers/SplitContainer.cs

+2
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ public override BindingContext? BindingContext
219219
{
220220
return BindingContextInternal;
221221
}
222+
223+
[RequiresUnreferencedCode(IBindableComponent.ComponentModelTrimIncompatibilityMessage)]
222224
set
223225
{
224226
BindingContextInternal = value;

0 commit comments

Comments
 (0)