Skip to content

Commit 2193467

Browse files
authored
Guard ToolboxItem by a new feature switch (#99647)
This gets rid of the remaining references to System.Windows.Forms.Design in a trimmed empty winforms app, when the feature switch is set to false.
1 parent eb864bf commit 2193467

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/libraries/System.ComponentModel.TypeConverter/src/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@
172172
<data name="CultureInfoConverterInvalidCulture" xml:space="preserve">
173173
<value>The {0} culture cannot be converted to a CultureInfo object on this computer.</value>
174174
</data>
175+
<data name="IDesignerHostNotSupported" xml:space="preserve">
176+
<value>Designer support has been disabled in the app configuration and is not supported.</value>
177+
</data>
175178
<data name="ErrorInvalidServiceInstance" xml:space="preserve">
176179
<value>The service instance must derive from or implement {0}.</value>
177180
</data>

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/Design/IDesignerHost.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace System.ComponentModel.Design
57
{
68
/// <summary>
@@ -9,6 +11,9 @@ namespace System.ComponentModel.Design
911
/// </summary>
1012
public interface IDesignerHost : IServiceContainer
1113
{
14+
[FeatureSwitchDefinition("System.ComponentModel.Design.IDesignerHost.IsSupported")]
15+
internal static bool IsSupported => AppContext.TryGetSwitch("System.ComponentModel.Design.IDesignerHost.IsSupported", out bool isSupported) ? isSupported : true;
16+
1217
/// <summary>
1318
/// Gets or sets a value indicating whether the designer host
1419
/// is currently loading the document.

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ToolboxItemAttribute.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.ComponentModel.Design;
45
using System.Diagnostics.CodeAnalysis;
56

67
namespace System.ComponentModel
@@ -16,10 +17,12 @@ public class ToolboxItemAttribute : Attribute
1617
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
1718
private readonly string? _toolboxItemTypeName;
1819

20+
private const string DefaultToolboxItemTypeName = "System.Drawing.Design.ToolboxItem, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
21+
1922
/// <summary>
2023
/// Initializes a new instance of ToolboxItemAttribute and sets the type to
2124
/// </summary>
22-
public static readonly ToolboxItemAttribute Default = new ToolboxItemAttribute("System.Drawing.Design.ToolboxItem, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
25+
public static readonly ToolboxItemAttribute Default = new ToolboxItemAttribute(DefaultToolboxItemTypeName);
2326

2427
/// <summary>
2528
/// Initializes a new instance of ToolboxItemAttribute and sets the type to
@@ -30,7 +33,7 @@ public class ToolboxItemAttribute : Attribute
3033
/// <summary>
3134
/// Gets whether the attribute is the default attribute.
3235
/// </summary>
33-
public override bool IsDefaultAttribute() => Equals(Default);
36+
public override bool IsDefaultAttribute() => _toolboxItemTypeName == DefaultToolboxItemTypeName;
3437

3538
/// <summary>
3639
/// Initializes a new instance of ToolboxItemAttribute and specifies if default values should be used.
@@ -39,7 +42,12 @@ public ToolboxItemAttribute(bool defaultType)
3942
{
4043
if (defaultType)
4144
{
42-
_toolboxItemTypeName = "System.Drawing.Design.ToolboxItem, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
45+
if (!IDesignerHost.IsSupported)
46+
{
47+
throw new NotSupportedException(SR.IDesignerHostNotSupported);
48+
}
49+
50+
_toolboxItemTypeName = DefaultToolboxItemTypeName;
4351
}
4452
}
4553

0 commit comments

Comments
 (0)