@@ -99,8 +99,7 @@ public StringValue ( string name, string? value ) {
9999 /// </summary>
100100 /// <typeparam name="TEnum">The <see cref="Enum"/> type.</typeparam>
101101 public TEnum GetEnum < TEnum > ( ) where TEnum : struct , Enum {
102- ThrowIfNull ( ) ;
103- return Enum . Parse < TEnum > ( _ref ! , true ) ;
102+ return Enum . Parse < TEnum > ( GetString ( ) , true ) ;
104103 }
105104
106105 /// <summary>
@@ -110,184 +109,176 @@ public TEnum GetEnum<TEnum> () where TEnum : struct, Enum {
110109 /// <returns>An non-null string value.</returns>
111110 /// <exception cref="NullReferenceException">Thrown when the value stored in this instance is null.</exception>
112111 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 ;
115115 }
116116
117117 /// <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 .
120120 /// </summary>
121- /// <returns>An non-null char value .</returns>
121+ /// <returns>The converted <see cref="char"/> .</returns>
122122 public char GetChar ( ) {
123- ThrowIfNull ( ) ;
124- if ( _ref ! . Length != 1 )
123+ string r = GetString ( ) ;
124+ if ( r . Length != 1 )
125125 throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , "char" ) ) ;
126126
127- return _ref [ 0 ] ;
127+ return r [ 0 ] ;
128128 }
129129
130130 /// <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.
132133 /// </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 ;
143140 }
141+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Int32 ) ) ) ;
144142 }
145143
146144 /// <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.
148147 /// </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 ;
159154 }
155+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Byte ) ) ) ;
160156 }
161157
162158 /// <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.
164161 /// </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 ;
175168 }
169+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Int64 ) ) ) ;
176170 }
177171
178172 /// <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.
180175 /// </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 ;
192182 }
183+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Int16 ) ) ) ;
193184 }
194185
195186 /// <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.
197189 /// </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 ;
209196 }
197+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Double ) ) ) ;
210198 }
211199
212200 /// <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.
214203 /// </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 ;
226210 }
211+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Single ) ) ) ;
227212 }
228213
229214 /// <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.
231217 /// </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 ;
242224 }
225+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Decimal ) ) ) ;
243226 }
244227
245228 /// <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.
247231 /// </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 ;
259237 }
238+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( Boolean ) ) ) ;
260239 }
261240
262241 /// <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.
264244 /// </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 ;
275251 }
252+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , nameof ( DateTime ) ) ) ;
276253 }
277254
278255 /// <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.
280258 /// </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 ) ) ) ;
285267 }
286268
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 ;
290280 }
281+ throw new FormatException ( SR . Format ( SR . ValueItem_CastException , _ref , argName , typeof ( T ) . Name ) ) ;
291282 }
292283
293284 /// <inheritdoc/>
@@ -303,7 +294,7 @@ void ThrowIfNull () {
303294 /// <inheritdoc/>
304295 /// <exclude/>
305296 public static bool operator != ( StringValue i , object ? other ) {
306- return ! i . Equals ( other ) ;
297+ return ! ( i == other ) ;
307298 }
308299
309300 /// <inheritdoc/>
0 commit comments