diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/Resources/Strings.resx b/src/libraries/System.ComponentModel.TypeConverter/src/Resources/Strings.resx index 6930398b398144..167b28a03e2b46 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/Resources/Strings.resx +++ b/src/libraries/System.ComponentModel.TypeConverter/src/Resources/Strings.resx @@ -103,6 +103,9 @@ The specified type is not a nullable type. + + Runtime instantiation of this attribute is not allowed. + (Text) diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/AmbientValueAttribute.cs b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/AmbientValueAttribute.cs index 79b4d31603b988..53a43204668305 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/AmbientValueAttribute.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/AmbientValueAttribute.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.ComponentModel.Design; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; namespace System.ComponentModel @@ -12,6 +14,11 @@ namespace System.ComponentModel [AttributeUsage(AttributeTargets.All)] public sealed class AmbientValueAttribute : Attribute { + /// + /// This is the default value. + /// + private object? _value; + /// /// Initializes a new instance of the class, converting the /// specified value to the specified type, and using the U.S. English culture as the @@ -22,9 +29,15 @@ public AmbientValueAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemb { // The try/catch here is because attributes should never throw exceptions. We would fail to // load an otherwise normal class. + + if (!IDesignerHost.IsSupported) + { + return; + } + try { - Value = TypeDescriptor.GetConverter(type).ConvertFromInvariantString(value); + _value = TypeDescriptor.GetConverter(type).ConvertFromInvariantString(value); } catch { @@ -37,7 +50,7 @@ public AmbientValueAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemb /// public AmbientValueAttribute(char value) { - Value = value; + _value = value; } /// @@ -46,7 +59,7 @@ public AmbientValueAttribute(char value) /// public AmbientValueAttribute(byte value) { - Value = value; + _value = value; } /// @@ -55,7 +68,7 @@ public AmbientValueAttribute(byte value) /// public AmbientValueAttribute(short value) { - Value = value; + _value = value; } /// @@ -64,7 +77,7 @@ public AmbientValueAttribute(short value) /// public AmbientValueAttribute(int value) { - Value = value; + _value = value; } /// @@ -73,7 +86,7 @@ public AmbientValueAttribute(int value) /// public AmbientValueAttribute(long value) { - Value = value; + _value = value; } /// @@ -82,7 +95,7 @@ public AmbientValueAttribute(long value) /// public AmbientValueAttribute(float value) { - Value = value; + _value = value; } /// @@ -91,7 +104,7 @@ public AmbientValueAttribute(float value) /// public AmbientValueAttribute(double value) { - Value = value; + _value = value; } /// @@ -100,7 +113,7 @@ public AmbientValueAttribute(double value) /// public AmbientValueAttribute(bool value) { - Value = value; + _value = value; } /// @@ -108,7 +121,7 @@ public AmbientValueAttribute(bool value) /// public AmbientValueAttribute(string? value) { - Value = value; + _value = value; } /// @@ -117,13 +130,22 @@ public AmbientValueAttribute(string? value) /// public AmbientValueAttribute(object? value) { - Value = value; + _value = value; } /// /// Gets the ambient value of the property this attribute is bound to. /// - public object? Value { get; } + public object? Value { + get + { + if (!IDesignerHost.IsSupported) + { + throw new ArgumentException(SR.RuntimeInstanceNotAllowed); + } + return _value; + } + } public override bool Equals([NotNullWhen(true)] object? obj) { diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx index bbc4b27c51ddeb..2bc7699526df53 100644 --- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx +++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx @@ -3359,6 +3359,9 @@ Non-static method requires a target. + + Runtime instantiation of this attribute is not allowed. + An object that does not derive from System.Exception has been wrapped in a RuntimeWrappedException. @@ -4313,4 +4316,4 @@ Blocking wait is not supported on the JS interop threads. - \ No newline at end of file + diff --git a/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs index e86b53d640d30f..69411fcc9f4435 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Reflection; @@ -22,6 +23,9 @@ public class DefaultValueAttribute : Attribute // Delegate ad hoc created 'TypeDescriptor.ConvertFromInvariantString' reflection object cache private static object? s_convertFromInvariantString; + [FeatureSwitchDefinition("System.ComponentModel.DefaultValueAttribute.IsSupported")] + internal static bool IsSupported => AppContext.TryGetSwitch("System.ComponentModel.DefaultValueAttribute.IsSupported", out bool isSupported) ? isSupported : true; + /// /// Initializes a new instance of the /// class, converting the specified value to the specified type, and using the U.S. English @@ -35,6 +39,12 @@ public DefaultValueAttribute( // The null check and try/catch here are because attributes should never throw exceptions. // We would fail to load an otherwise normal class. + if (!IsSupported) + { + Debug.Assert(!IsSupported, "Runtime instantiation of this attribute is not allowed."); + return; + } + if (type == null) { return; @@ -229,7 +239,18 @@ public DefaultValueAttribute(ulong value) /// /// Gets the default value of the property this attribute is bound to. /// - public virtual object? Value => _value; + public virtual object? Value + { + get + { + if (!IsSupported) + { + throw new ArgumentException(SR.RuntimeInstanceNotAllowed); + } + return _value; + } + } + public override bool Equals([NotNullWhen(true)] object? obj) {