@@ -99,8 +99,7 @@ public StringValue ( string name, string? value ) {
99
99
/// </summary>
100
100
/// <typeparam name="TEnum">The <see cref="Enum"/> type.</typeparam>
101
101
public TEnum GetEnum < TEnum > ( ) where TEnum : struct , Enum {
102
- ThrowIfNull ( ) ;
103
- return Enum . Parse < TEnum > ( _ref ! , true ) ;
102
+ return Enum . Parse < TEnum > ( GetString ( ) , true ) ;
104
103
}
105
104
106
105
/// <summary>
@@ -110,184 +109,176 @@ public TEnum GetEnum<TEnum> () where TEnum : struct, Enum {
110
109
/// <returns>An non-null string value.</returns>
111
110
/// <exception cref="NullReferenceException">Thrown when the value stored in this instance is null.</exception>
112
111
public string GetString ( ) {
113
- ThrowIfNull ( ) ;
114
- return _ref ! ;
112
+ if ( _ref is null )
113
+ throw new InvalidOperationException ( SR . Format ( SR . ValueItem_ValueNull , argName , argType ) ) ;
114
+ return _ref ;
115
115
}
116
116
117
117
/// <summary>
118
- /// Gets a <see cref="char"/> from this <see cref="StringValue"/>. This method will throw an <see cref="NullReferenceException "/> if
119
- /// the value stored in this instance is null .
118
+ /// Parses the value contained in this <see cref="StringValue"/> as a <see cref="char "/>. Throws an exception
119
+ /// if the value couldn't be parsed to the target type .
120
120
/// </summary>
121
- /// <returns>An non-null char value .</returns>
121
+ /// <returns>The converted <see cref="char"/> .</returns>
122
122
public char GetChar ( ) {
123
- ThrowIfNull ( ) ;
124
- if ( _ref ! . Length != 1 )
123
+ string r = GetString ( ) ;
124
+ if ( r . Length != 1 )
125
125
throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , "char" ) ) ;
126
126
127
- return _ref [ 0 ] ;
127
+ return r [ 0 ] ;
128
128
}
129
129
130
130
/// <summary>
131
- /// Gets a <see cref="Int32"/> from this <see cref="StringValue"/>.
131
+ /// Parses the value contained in this <see cref="StringValue"/> as an <see cref="int"/>. Throws an exception
132
+ /// if the value couldn't be parsed to the target type.
132
133
/// </summary>
133
- /// <returns>An non-null Int32 value.</returns>
134
- /// <exception cref="NullReferenceException">Thrown when the value stored in this instance is null.</exception>
135
- /// <exception cref="FormatException">Thrown when the value stored in this instance is not parseable to the desired type.</exception>
136
- public int GetInteger ( ) {
137
- ThrowIfNull ( ) ;
138
- try {
139
- return int . Parse ( _ref ! , provider : null ) ;
140
- }
141
- catch ( Exception ex ) when ( ex is FormatException || ex is InvalidCastException ) {
142
- throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , "integer" ) ) ;
134
+ /// <param name="formatProvider">The <see cref="IFormatProvider"/> to use for parsing. Defaults to <see langword="null"/>.</param>
135
+ /// <returns>The converted <see cref="int"/>.</returns>
136
+ /// <exception cref="FormatException">Thrown when the value cannot be parsed to an <see cref="int"/>.</exception>
137
+ public int GetInteger ( IFormatProvider ? formatProvider = null ) {
138
+ if ( Int32 . TryParse ( GetString ( ) , formatProvider , out Int32 result ) ) {
139
+ return result ;
143
140
}
141
+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Int32 ) ) ) ;
144
142
}
145
143
146
144
/// <summary>
147
- /// Gets a <see cref="Byte"/> from this <see cref="StringValue"/>.
145
+ /// Parses the value contained in this <see cref="StringValue"/> as a <see cref="byte"/>. Throws an exception
146
+ /// if the value couldn't be parsed to the target type.
148
147
/// </summary>
149
- /// <returns>An non-null byte value.</returns>
150
- /// <exception cref="NullReferenceException">Thrown when the value stored in this instance is null.</exception>
151
- /// <exception cref="FormatException">Thrown when the value stored in this instance is not parseable to the desired type.</exception>
152
- public int GetByte ( ) {
153
- ThrowIfNull ( ) ;
154
- try {
155
- return byte . Parse ( _ref ! , provider : null ) ;
156
- }
157
- catch ( Exception ex ) when ( ex is FormatException || ex is InvalidCastException ) {
158
- throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , "byte" ) ) ;
148
+ /// <param name="formatProvider">The <see cref="IFormatProvider"/> to use for parsing. Defaults to <see langword="null"/>.</param>
149
+ /// <returns>The converted <see cref="byte"/>.</returns>
150
+ /// <exception cref="FormatException">Thrown when the value cannot be parsed to a <see cref="byte"/>.</exception>
151
+ public int GetByte ( IFormatProvider ? formatProvider = null ) {
152
+ if ( Byte . TryParse ( GetString ( ) , formatProvider , out Byte result ) ) {
153
+ return result ;
159
154
}
155
+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Byte ) ) ) ;
160
156
}
161
157
162
158
/// <summary>
163
- /// Gets a <see cref="Int64"/> from this <see cref="StringValue"/>.
159
+ /// Parses the value contained in this <see cref="StringValue"/> as a <see cref="long"/>. Throws an exception
160
+ /// if the value couldn't be parsed to the target type.
164
161
/// </summary>
165
- /// <returns>An non-null long value.</returns>
166
- /// <exception cref="NullReferenceException">Thrown when the value stored in this instance is null.</exception>
167
- /// <exception cref="FormatException">Thrown when the value stored in this instance is not parseable to the desired type.</exception>
168
- public long GetLong ( ) {
169
- ThrowIfNull ( ) ;
170
- try {
171
- return long . Parse ( _ref ! , provider : null ) ;
172
- }
173
- catch ( Exception ex ) when ( ex is FormatException || ex is InvalidCastException ) {
174
- throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , "long" ) ) ;
162
+ /// <param name="formatProvider">The <see cref="IFormatProvider"/> to use for parsing. Defaults to <see langword="null"/>.</param>
163
+ /// <returns>The converted <see cref="long"/>.</returns>
164
+ /// <exception cref="FormatException">Thrown when the value cannot be parsed to a <see cref="long"/>.</exception>
165
+ public long GetLong ( IFormatProvider ? formatProvider = null ) {
166
+ if ( Int64 . TryParse ( GetString ( ) , formatProvider , out Int64 result ) ) {
167
+ return result ;
175
168
}
169
+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Int64 ) ) ) ;
176
170
}
177
171
178
172
/// <summary>
179
- /// Gets a <see cref="Int16"/> from this <see cref="StringValue"/>.
173
+ /// Parses the value contained in this <see cref="StringValue"/> as a <see cref="short"/>. Throws an exception
174
+ /// if the value couldn't be parsed to the target type.
180
175
/// </summary>
181
- /// <param name="fmtProvider">Optional. Specifies the culture-specific format information.</param>
182
- /// <returns>An non-null short value.</returns>
183
- /// <exception cref="NullReferenceException">Thrown when the value stored in this instance is null.</exception>
184
- /// <exception cref="FormatException">Thrown when the value stored in this instance is not parseable to the desired type.</exception>
185
- public short GetShort ( IFormatProvider ? fmtProvider = null ) {
186
- ThrowIfNull ( ) ;
187
- try {
188
- return short . Parse ( _ref ! , fmtProvider ) ;
189
- }
190
- catch ( Exception ex ) when ( ex is FormatException || ex is InvalidCastException ) {
191
- throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , "short" ) ) ;
176
+ /// <param name="formatProvider">The <see cref="IFormatProvider"/> to use for parsing. Defaults to <see langword="null"/>.</param>
177
+ /// <returns>The converted <see cref="short"/>.</returns>
178
+ /// <exception cref="FormatException">Thrown when the value cannot be parsed to a <see cref="short"/>.</exception>
179
+ public short GetShort ( IFormatProvider ? formatProvider = null ) {
180
+ if ( Int16 . TryParse ( GetString ( ) , formatProvider , out Int16 result ) ) {
181
+ return result ;
192
182
}
183
+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Int16 ) ) ) ;
193
184
}
194
185
195
186
/// <summary>
196
- /// Gets a <see cref="double"/> from this <see cref="StringValue"/>.
187
+ /// Parses the value contained in this <see cref="StringValue"/> as a <see cref="double"/>. Throws an exception
188
+ /// if the value couldn't be parsed to the target type.
197
189
/// </summary>
198
- /// <param name="fmtProvider">Optional. Specifies the culture-specific format information.</param>
199
- /// <returns>An non-null double value.</returns>
200
- /// <exception cref="NullReferenceException">Thrown when the value stored in this instance is null.</exception>
201
- /// <exception cref="FormatException">Thrown when the value stored in this instance is not parseable to the desired type.</exception>
202
- public double GetDouble ( IFormatProvider ? fmtProvider = null ) {
203
- ThrowIfNull ( ) ;
204
- try {
205
- return double . Parse ( _ref ! , fmtProvider ) ;
206
- }
207
- catch ( Exception ex ) when ( ex is FormatException || ex is InvalidCastException ) {
208
- throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , "double" ) ) ;
190
+ /// <param name="formatProvider">The <see cref="IFormatProvider"/> to use for parsing. Defaults to <see langword="null"/>.</param>
191
+ /// <returns>The converted <see cref="double"/>.</returns>
192
+ /// <exception cref="FormatException">Thrown when the value cannot be parsed to a <see cref="double"/>.</exception>
193
+ public double GetDouble ( IFormatProvider ? formatProvider = null ) {
194
+ if ( Double . TryParse ( GetString ( ) , formatProvider , out Double result ) ) {
195
+ return result ;
209
196
}
197
+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Double ) ) ) ;
210
198
}
211
199
212
200
/// <summary>
213
- /// Gets a <see cref="float"/> from this <see cref="StringValue"/>.
201
+ /// Parses the value contained in this <see cref="StringValue"/> as a <see cref="float"/>. Throws an exception
202
+ /// if the value couldn't be parsed to the target type.
214
203
/// </summary>
215
- /// <param name="fmtProvider">Optional. Specifies the culture-specific format information.</param>
216
- /// <returns>An non-null double value.</returns>
217
- /// <exception cref="NullReferenceException">Thrown when the value stored in this instance is null.</exception>
218
- /// <exception cref="FormatException">Thrown when the value stored in this instance is not parseable to the desired type.</exception>
219
- public double GetSingle ( IFormatProvider ? fmtProvider = null ) {
220
- ThrowIfNull ( ) ;
221
- try {
222
- return float . Parse ( _ref ! , fmtProvider ) ;
223
- }
224
- catch ( Exception ex ) when ( ex is FormatException || ex is InvalidCastException ) {
225
- throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , "float" ) ) ;
204
+ /// <param name="formatProvider">The <see cref="IFormatProvider"/> to use for parsing. Defaults to <see langword="null"/>.</param>
205
+ /// <returns>The converted <see cref="float"/>.</returns>
206
+ /// <exception cref="FormatException">Thrown when the value cannot be parsed to a <see cref="float"/>.</exception>
207
+ public float GetSingle ( IFormatProvider ? formatProvider = null ) {
208
+ if ( Single . TryParse ( GetString ( ) , formatProvider , out Single result ) ) {
209
+ return result ;
226
210
}
211
+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Single ) ) ) ;
227
212
}
228
213
229
214
/// <summary>
230
- /// Gets a <see cref="bool"/> from this <see cref="StringValue"/>.
215
+ /// Parses the value contained in this <see cref="StringValue"/> as a <see cref="decimal"/>. Throws an exception
216
+ /// if the value couldn't be parsed to the target type.
231
217
/// </summary>
232
- /// <returns>An non-null boolean value.</returns>
233
- /// <exception cref="NullReferenceException">Thrown when the value stored in this instance is null.</exception>
234
- /// <exception cref="FormatException">Thrown when the value stored in this instance is not parseable to the desired type.</exception>
235
- public bool GetBoolean ( ) {
236
- ThrowIfNull ( ) ;
237
- try {
238
- return bool . Parse ( _ref ! ) ;
239
- }
240
- catch ( Exception ex ) when ( ex is FormatException || ex is InvalidCastException ) {
241
- throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , "boolean" ) ) ;
218
+ /// <param name="formatProvider">The <see cref="IFormatProvider"/> to use for parsing. Defaults to <see langword="null"/>.</param>
219
+ /// <returns>The converted <see cref="decimal"/>.</returns>
220
+ /// <exception cref="FormatException">Thrown when the value cannot be parsed to a <see cref="decimal"/>.</exception>
221
+ public decimal GetDecimal ( IFormatProvider ? formatProvider = null ) {
222
+ if ( Decimal . TryParse ( GetString ( ) , formatProvider , out Decimal result ) ) {
223
+ return result ;
242
224
}
225
+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Decimal ) ) ) ;
243
226
}
244
227
245
228
/// <summary>
246
- /// Gets a <see cref="DateTime"/> from this <see cref="StringValue"/>.
229
+ /// Parses the value contained in this <see cref="StringValue"/> as a <see cref="bool"/>. Throws an exception
230
+ /// if the value couldn't be parsed to the target type.
247
231
/// </summary>
248
- /// <param name="fmtProvider">Optional. Specifies the culture-specific format information.</param>
249
- /// <returns>An non-null DateTime value.</returns>
250
- /// <exception cref="NullReferenceException">Thrown when the value stored in this instance is null.</exception>
251
- /// <exception cref="FormatException">Thrown when the value stored in this instance is not parseable to the desired type.</exception>
252
- public DateTime GetDateTime ( IFormatProvider ? fmtProvider = null ) {
253
- ThrowIfNull ( ) ;
254
- try {
255
- return DateTime . Parse ( _ref ! , fmtProvider ) ;
256
- }
257
- catch ( Exception ex ) when ( ex is FormatException || ex is InvalidCastException ) {
258
- throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , "DateTime" ) ) ;
232
+ /// <returns>The converted <see cref="bool"/>.</returns>
233
+ /// <exception cref="FormatException">Thrown when the value cannot be parsed to a <see cref="bool"/>.</exception>
234
+ public bool GetBoolean ( ) {
235
+ if ( Boolean . TryParse ( GetString ( ) , out Boolean result ) ) {
236
+ return result ;
259
237
}
238
+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Boolean ) ) ) ;
260
239
}
261
240
262
241
/// <summary>
263
- /// Gets a <see cref="Guid"/> from this <see cref="StringValue"/>.
242
+ /// Parses the value contained in this <see cref="StringValue"/> as a <see cref="DateTime"/>. Throws an exception
243
+ /// if the value couldn't be parsed to the target type.
264
244
/// </summary>
265
- /// <returns>An non-null Guid value.</returns>
266
- /// <exception cref="NullReferenceException">Thrown when the value stored in this instance is null.</exception>
267
- /// <exception cref="FormatException">Thrown when the value stored in this instance is not parseable to the desired type.</exception>
268
- public Guid GetGuid ( ) {
269
- ThrowIfNull ( ) ;
270
- try {
271
- return Guid . Parse ( _ref ! ) ;
272
- }
273
- catch ( Exception ex ) when ( ex is FormatException || ex is InvalidCastException ) {
274
- throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , "GUID" ) ) ;
245
+ /// <param name="formatProvider">The <see cref="IFormatProvider"/> to use for parsing. Defaults to <see langword="null"/>.</param>
246
+ /// <returns>The converted <see cref="DateTime"/>.</returns>
247
+ /// <exception cref="FormatException">Thrown when the value cannot be parsed to a <see cref="DateTime"/>.</exception>
248
+ public DateTime GetDateTime ( IFormatProvider ? formatProvider = null ) {
249
+ if ( DateTime . TryParse ( GetString ( ) , formatProvider , out DateTime result ) ) {
250
+ return result ;
275
251
}
252
+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( DateTime ) ) ) ;
276
253
}
277
254
278
255
/// <summary>
279
- /// Gets an not null value from the specified <typeparamref name="T"/>.
256
+ /// Parses the value contained in this <see cref="StringValue"/> as a <see cref="Guid"/>. Throws an exception
257
+ /// if the value couldn't be parsed to the target type.
280
258
/// </summary>
281
- /// <typeparam name="T">The type to convert the value to.</typeparam>
282
- public T Get < T > ( IFormatProvider ? fmtProvider = null ) where T : IParsable < T > {
283
- ThrowIfNull ( ) ;
284
- return T . Parse ( _ref ! , fmtProvider ) ;
259
+ /// <param name="formatProvider">The <see cref="IFormatProvider"/> to use for parsing. Defaults to <see langword="null"/>.</param>
260
+ /// <returns>The converted <see cref="Guid"/>.</returns>
261
+ /// <exception cref="FormatException">Thrown when the value cannot be parsed to a <see cref="Guid"/>.</exception>
262
+ public Guid GetGuid ( IFormatProvider ? formatProvider = null ) {
263
+ if ( Guid . TryParse ( GetString ( ) , formatProvider , out Guid result ) ) {
264
+ return result ;
265
+ }
266
+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Guid ) ) ) ;
285
267
}
286
268
287
- void ThrowIfNull ( ) {
288
- if ( IsNull ) {
289
- throw new InvalidOperationException ( SR . Format ( SR . ValueItem_ValueNull , argName , argType ) ) ;
269
+ /// <summary>
270
+ /// Parses the value contained in this <see cref="StringValue"/> as a type <typeparamref name="T"/> that implements <see cref="IParsable{T}"/>.
271
+ /// Throws an exception if the value couldn't be parsed to the target type.
272
+ /// </summary>
273
+ /// <typeparam name="T">The type to parse the value to.</typeparam>
274
+ /// <param name="formatProvider">The <see cref="IFormatProvider"/> to use for parsing. Defaults to <see langword="null"/>.</param>
275
+ /// <returns>The converted value of type <typeparamref name="T"/>.</returns>
276
+ /// <exception cref="FormatException">Thrown when the value cannot be parsed to type <typeparamref name="T"/>.</exception>
277
+ public T Get < T > ( IFormatProvider ? formatProvider = null ) where T : IParsable < T > {
278
+ if ( T . TryParse ( GetString ( ) , formatProvider , out T ? result ) ) {
279
+ return result ;
290
280
}
281
+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , typeof ( T ) . Name ) ) ;
291
282
}
292
283
293
284
/// <inheritdoc/>
@@ -303,7 +294,7 @@ void ThrowIfNull () {
303
294
/// <inheritdoc/>
304
295
/// <exclude/>
305
296
public static bool operator != ( StringValue i , object ? other ) {
306
- return ! i . Equals ( other ) ;
297
+ return ! ( i == other ) ;
307
298
}
308
299
309
300
/// <inheritdoc/>
0 commit comments