1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . ComponentModel . Design ;
5
+ using System . Diagnostics ;
4
6
using System . Diagnostics . CodeAnalysis ;
5
7
6
8
namespace System . ComponentModel
@@ -12,6 +14,11 @@ namespace System.ComponentModel
12
14
[ AttributeUsage ( AttributeTargets . All ) ]
13
15
public sealed class AmbientValueAttribute : Attribute
14
16
{
17
+ /// <summary>
18
+ /// This is the default value.
19
+ /// </summary>
20
+ private object ? _value ;
21
+
15
22
/// <summary>
16
23
/// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class, converting the
17
24
/// specified value to the specified type, and using the U.S. English culture as the
@@ -22,9 +29,15 @@ public AmbientValueAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemb
22
29
{
23
30
// The try/catch here is because attributes should never throw exceptions. We would fail to
24
31
// load an otherwise normal class.
32
+
33
+ if ( ! IDesignerHost . IsSupported )
34
+ {
35
+ return ;
36
+ }
37
+
25
38
try
26
39
{
27
- Value = TypeDescriptor . GetConverter ( type ) . ConvertFromInvariantString ( value ) ;
40
+ _value = TypeDescriptor . GetConverter ( type ) . ConvertFromInvariantString ( value ) ;
28
41
}
29
42
catch
30
43
{
@@ -37,7 +50,7 @@ public AmbientValueAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemb
37
50
/// </summary>
38
51
public AmbientValueAttribute ( char value )
39
52
{
40
- Value = value ;
53
+ _value = value ;
41
54
}
42
55
43
56
/// <summary>
@@ -46,7 +59,7 @@ public AmbientValueAttribute(char value)
46
59
/// </summary>
47
60
public AmbientValueAttribute ( byte value )
48
61
{
49
- Value = value ;
62
+ _value = value ;
50
63
}
51
64
52
65
/// <summary>
@@ -55,7 +68,7 @@ public AmbientValueAttribute(byte value)
55
68
/// </summary>
56
69
public AmbientValueAttribute ( short value )
57
70
{
58
- Value = value ;
71
+ _value = value ;
59
72
}
60
73
61
74
/// <summary>
@@ -64,7 +77,7 @@ public AmbientValueAttribute(short value)
64
77
/// </summary>
65
78
public AmbientValueAttribute ( int value )
66
79
{
67
- Value = value ;
80
+ _value = value ;
68
81
}
69
82
70
83
/// <summary>
@@ -73,7 +86,7 @@ public AmbientValueAttribute(int value)
73
86
/// </summary>
74
87
public AmbientValueAttribute ( long value )
75
88
{
76
- Value = value ;
89
+ _value = value ;
77
90
}
78
91
79
92
/// <summary>
@@ -82,7 +95,7 @@ public AmbientValueAttribute(long value)
82
95
/// </summary>
83
96
public AmbientValueAttribute ( float value )
84
97
{
85
- Value = value ;
98
+ _value = value ;
86
99
}
87
100
88
101
/// <summary>
@@ -91,7 +104,7 @@ public AmbientValueAttribute(float value)
91
104
/// </summary>
92
105
public AmbientValueAttribute ( double value )
93
106
{
94
- Value = value ;
107
+ _value = value ;
95
108
}
96
109
97
110
/// <summary>
@@ -100,15 +113,15 @@ public AmbientValueAttribute(double value)
100
113
/// </summary>
101
114
public AmbientValueAttribute ( bool value )
102
115
{
103
- Value = value ;
116
+ _value = value ;
104
117
}
105
118
106
119
/// <summary>
107
120
/// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a <see cref='string'/>.
108
121
/// </summary>
109
122
public AmbientValueAttribute ( string ? value )
110
123
{
111
- Value = value ;
124
+ _value = value ;
112
125
}
113
126
114
127
/// <summary>
@@ -117,13 +130,22 @@ public AmbientValueAttribute(string? value)
117
130
/// </summary>
118
131
public AmbientValueAttribute ( object ? value )
119
132
{
120
- Value = value ;
133
+ _value = value ;
121
134
}
122
135
123
136
/// <summary>
124
137
/// Gets the ambient value of the property this attribute is bound to.
125
138
/// </summary>
126
- public object ? Value { get ; }
139
+ public object ? Value {
140
+ get
141
+ {
142
+ if ( ! IDesignerHost . IsSupported )
143
+ {
144
+ throw new ArgumentException ( SR . RuntimeInstanceNotAllowed ) ;
145
+ }
146
+ return _value ;
147
+ }
148
+ }
127
149
128
150
public override bool Equals ( [ NotNullWhen ( true ) ] object ? obj )
129
151
{
0 commit comments