Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bubble up formatter trim warnings #11023

Merged
merged 9 commits into from
Mar 18, 2024
Merged
5 changes: 4 additions & 1 deletion src/System.Windows.Forms/src/Resources/SR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6964,4 +6964,7 @@ Stack trace where the illegal operation occurred was:
<data name="FolderBrowserDialogMultiSelectDescr" xml:space="preserve">
<value>Controls whether multiple folders can be selected in the dialog.</value>
</data>
</root>
<data name="BindingNotSupported" xml:space="preserve">
<value>Binding operations are not supported when application trimming is enabled.</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@ public void ResetBindings()
public virtual BindingContext? BindingContext
{
get => BindingContextInternal;
[RequiresUnreferencedCode(IBindableComponent.ComponentModelTrimIncompatibilityMessage)]
set => BindingContextInternal = value;
}

Expand Down Expand Up @@ -7052,6 +7053,11 @@ protected virtual void OnBindingContextChanged(EventArgs e)
{
if (Properties.ContainsObjectThatIsNotNull(s_bindingsProperty))
{
if (!Binding.IsSupported)
{
throw new NotSupportedException(SR.BindingNotSupported);
}

UpdateBindings();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,11 @@ private void SetDataConnection(object? newDataSource, BindingMemberInfo newDispl
CurrencyManager? newDataManager = null;
if (newDataSource is not null && BindingContext is not null && newDataSource != Convert.DBNull)
{
if (!Binding.IsSupported)
{
throw new NotSupportedException(SR.BindingNotSupported);
}

newDataManager = (CurrencyManager)BindingContext[newDataSource, newDisplayMember.BindingPath];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ public new Color BackColor
// we don't have a binding context
return null;
}

[RequiresUnreferencedCode(IBindableComponent.ComponentModelTrimIncompatibilityMessage)]
set
{
if (Properties.GetObject(s_propBindingContext) != value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public abstract class BindableComponent : Component, IBindableComponent
{
get => _bindingContext ??= [];

[RequiresUnreferencedCode(IBindableComponent.ComponentModelTrimIncompatibilityMessage)]
set
{
if (!Equals(_bindingContext, value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ namespace System.Windows.Forms;
[TypeConverter(typeof(ListBindingConverter))]
public partial class Binding
{
[FeatureSwitchDefinition("System.Windows.Forms.Binding.IsSupported")]
#pragma warning disable IDE0075 // Simplify conditional expression - the simpler expression is hard to read
internal static bool IsSupported => AppContext.TryGetSwitch("System.Windows.Forms.Binding.IsSupported", out bool isSupported) ? isSupported : true;
#pragma warning restore IDE0075 //Simplify conditional expression

private BindingManagerBase? _bindingManagerBase;

private readonly BindToObject _bindToObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ namespace System.Windows.Forms;

public interface IBindableComponent : IComponent
{
internal const string ComponentModelTrimIncompatibilityMessage = "Binding is not supported with trimming";
ControlBindingsCollection DataBindings { get; }
BindingContext? BindingContext { get; set; }
BindingContext? BindingContext
{
get;
[RequiresUnreferencedCode(ComponentModelTrimIncompatibilityMessage)]
set;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ namespace System.Windows.Forms;

internal static class Formatter
{
internal const string ComponentModelTrimIncompatibilityMessage = "ComponentModel APIs are not trimming compatible.";

private static readonly Type s_stringType = typeof(string);
private static readonly Type s_booleanType = typeof(bool);
private static readonly Type s_checkStateType = typeof(CheckState);
private static readonly object s_parseMethodNotFound = new();
private static readonly object s_defaultDataSourceNullValue = DBNull.Value;

/// <summary>
/// Converts a binary value into a format suitable for display to the end user.
/// Used when pushing a value from a back-end data source into a data-bound property on a control.
Expand All @@ -25,6 +26,7 @@ internal static class Formatter
/// If the caller is expecting a nullable value back, we must also re-wrap the final result
/// inside a nullable value before returning.
/// </summary>
[RequiresUnreferencedCode(ComponentModelTrimIncompatibilityMessage)]
public static object? FormatObject(
object? value,
Type targetType,
Expand Down Expand Up @@ -82,6 +84,7 @@ internal static class Formatter
/// - Uses TypeConverters or IConvertible where appropriate
/// - Throws a FormatException is no suitable conversion can be found
/// </summary>
[RequiresUnreferencedCode(ComponentModelTrimIncompatibilityMessage)]
private static object? FormatObjectInternal(
object? value,
Type targetType,
Expand Down Expand Up @@ -431,7 +434,7 @@ private static CultureInfo GetFormatterCulture(IFormatProvider? formatInfo)
/// <summary>
/// Converts a value to the specified type using best Parse() method on that type
/// </summary>
public static object? InvokeStringParseMethod(object? value, Type targetType, IFormatProvider? formatInfo)
public static object? InvokeStringParseMethod(object? value, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type targetType, IFormatProvider? formatInfo)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ public virtual AutoValidate AutoValidate

return bm;
}

[RequiresUnreferencedCode(IBindableComponent.ComponentModelTrimIncompatibilityMessage)]
set => base.BindingContext = value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ public override ImageLayout BackgroundImageLayout
{
return BindingContextInternal;
}

[RequiresUnreferencedCode(IBindableComponent.ComponentModelTrimIncompatibilityMessage)]
set
{
BindingContextInternal = value;
Expand Down
Loading