@@ -46,9 +46,10 @@ public EnumValue(string value)
4646 static readonly Func < string , TEnum > Parser = CreateParseEnumDelegate ( ) ;
4747
4848 /// <summary>
49- /// Uses a case-insenstive dictionary lookup to get a matching enum value.
49+ /// Uses an expression tree switch to get a matching enum value.
5050 /// </summary>
51- /// <returns>The enum the represents the string <paramref name="value"/> provided.</returns>
51+ /// <param name="value">The string represnting the enum to search for.</param>
52+ /// <returns>The enum that represents the string <paramref name="value"/> provided.</returns>
5253 /// <exception cref="ArgumentNullException">value is null</exception>
5354 /// <exception cref="ArgumentException">Requested <paramref name="value"/> was not found.</exception>
5455 public static TEnum Parse ( string value )
@@ -67,7 +68,7 @@ public static TEnum Parse(string value)
6768 }
6869
6970 /// <summary>
70- /// Uses a case-insenstive dictionary lookup to get a matching enum value.
71+ /// Uses an expression tree switch to get a matching enum value.
7172 /// </summary>
7273 /// <returns>true if the value found; otherwise false.</returns>
7374 /// <exception cref="ArgumentNullException"/>
@@ -126,7 +127,7 @@ static Func<string, TEnum> CreateParseEnumDelegate()
126127 /// Indicates whether this instance matches the provided enum <paramref name="value"/>.
127128 /// </summary>
128129 /// <returns>true if <paramref name="value"/> and this instance's enum value are the same; otherwise false.</returns>
129- public bool Equals ( TEnum value ) => Value . Equals ( value ) ;
130+ public bool Equals ( TEnum value ) => Value . Equals ( value ) ;
130131 public static bool operator == ( EnumValue < TEnum > left , TEnum right ) => left . Value . Equals ( right ) ;
131132 public static bool operator != ( EnumValue < TEnum > left , TEnum right ) => ! left . Value . Equals ( right ) ;
132133
@@ -172,7 +173,7 @@ public EnumValueCaseIgnored(TEnum value)
172173 /// <summary>
173174 /// Parses the string value to construct an EnumValueCaseIgnored<<typeparamref name="TEnum"/>> instance.
174175 /// </summary>
175- /// <exception cref="ArgumentNullException">value is null.</exception>
176+ /// <exception cref="ArgumentNullException"><paramref name=" value"/> is null.</exception>
176177 public EnumValueCaseIgnored ( string value )
177178 {
178179 Value = Parse ( value ) ;
@@ -186,30 +187,21 @@ public EnumValueCaseIgnored(string value)
186187 internal static readonly ImmutableDictionary < string , TEnum > CaseInsensitiveLookup
187188 = CreateCaseInsensitiveDictionary ( ) ;
188189
189- /// <inheritdoc cref="EnumValue{TEnum}.Parse(string)" />
190190 /// <summary>
191- /// Uses an expression tree switch to get a matching enum value.
191+ /// Uses a case-insenstive dictionary lookup to get a matching enum value.
192192 /// </summary>
193+ /// <inheritdoc cref="EnumValue{TEnum}.Parse(string)" />
193194 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
194195 public static TEnum Parse ( string value )
195- {
196- try
197- {
198- return CaseInsensitiveLookup [ value ] ;
199- }
200- catch ( KeyNotFoundException ex )
201- {
202- throw new ArgumentException ( $ "Requested value '{ value } ' was not found.", nameof ( value ) , ex ) ;
203- }
204- }
196+ => TryParse ( value , out var e ) ? e : throw new ArgumentException ( $ "Requested value '{ value } ' was not found.", nameof ( value ) ) ;
205197
206- /// <inheritdoc cref="EnumValue{TEnum}.TryParse(string, out TEnum)"/>
207198 /// <summary>
208- /// Uses an expression tree switch to get a matching enum value.
199+ /// Uses a case-insenstive dictionary lookup to get a matching enum value.
209200 /// </summary>
201+ /// <inheritdoc cref="EnumValue{TEnum}.TryParse(string, out TEnum)"/>
210202 public static bool TryParse ( string value , out TEnum e )
211203 {
212- if ( CaseInsensitiveLookup . TryGetValue ( value , out e ! ) ) return true ;
204+ if ( CaseInsensitiveLookup . TryGetValue ( value , out e ! ) ) return true ;
213205 e = default ! ;
214206 return false ;
215207 }
@@ -258,4 +250,41 @@ private string GetDebuggerDisplay()
258250 return $ "{ eType . Name } .{ Value } [EnumValueCaseIgnored<{ eType . FullName } >]";
259251 }
260252 }
253+
254+ public static class EnumValue
255+ {
256+ /// <inheritdoc cref="EnumValue{TEnum}.Parse(string)"/>
257+ public static TEnum Parse < TEnum > ( string value )
258+ where TEnum : Enum
259+ => EnumValue < TEnum > . Parse ( value ) ;
260+
261+ /// <summary>
262+ /// Converts the string representation of the name of one or more enumerated constants to an equivalent enumerated object.
263+ /// A parameter specifies whether the operation is case-insensitive.
264+ /// </summary>
265+ /// <param name="ignoreCase">If true, will ignore case differences when looking for a match.</param>
266+ /// <inheritdoc cref="EnumValue{TEnum}.Parse(string)"/>
267+ public static TEnum Parse < TEnum > ( string value , bool ignoreCase )
268+ where TEnum : Enum
269+ => ignoreCase
270+ ? EnumValueCaseIgnored < TEnum > . Parse ( value )
271+ : EnumValue < TEnum > . Parse ( value ) ;
272+
273+ /// <inheritdoc cref="EnumValue{TEnum}.TryParse(string, out TEnum)"/>
274+ public static bool TryParse < TEnum > ( string value , out TEnum e )
275+ where TEnum : Enum
276+ => EnumValue < TEnum > . TryParse ( value , out e ) ;
277+
278+ /// <inheritdoc cref="EnumValue{TEnum}.TryParse(string, out TEnum)"/>
279+ /// <summary>
280+ /// Converts the string representation of the name of one or more enumerated constants to an equivalent enumerated object.
281+ /// A parameter specifies whether the operation is case-insensitive.
282+ /// </summary>
283+ /// <param name="ignoreCase">If true, will ignore case differences when looking for a match.</param>
284+ public static bool TryParse < TEnum > ( string value , bool ignoreCase , out TEnum e )
285+ where TEnum : Enum
286+ => ignoreCase
287+ ? EnumValueCaseIgnored < TEnum > . TryParse ( value , out e )
288+ : EnumValue < TEnum > . TryParse ( value , out e ) ;
289+ }
261290}
0 commit comments