@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers
7
7
{
8
8
using System ;
9
9
using System . Collections . Generic ;
10
+ using System . Collections . Immutable ;
10
11
using LightJson ;
11
12
12
13
/// <summary>
@@ -82,15 +83,15 @@ internal static string ToStringValue(this JsonValue jsonValue, string elementNam
82
83
/// <param name="jsonValue">The key value pair identifying the JSON value.</param>
83
84
/// <returns>The enum value contained within the JSON value.</returns>
84
85
internal static TEnum ToEnumValue < TEnum > ( this KeyValuePair < string , JsonValue > jsonValue )
85
- where TEnum : struct
86
+ where TEnum : struct , Enum
86
87
{
87
88
if ( ! jsonValue . Value . IsString )
88
89
{
89
90
throw new InvalidSettingsException ( $ "{ jsonValue . Key } must contain an enum (string) value") ;
90
91
}
91
92
92
93
TEnum result ;
93
- if ( ! Enum . TryParse ( jsonValue . Value . AsString , true , out result ) )
94
+ if ( ! EnumHelper < TEnum > . TryParse ( jsonValue . Value . AsString , out result ) )
94
95
{
95
96
throw new InvalidSettingsException ( $ "{ jsonValue . Key } cannot contain enum value '{ jsonValue . Value . AsString } '") ;
96
97
}
@@ -106,15 +107,15 @@ internal static TEnum ToEnumValue<TEnum>(this KeyValuePair<string, JsonValue> js
106
107
/// <param name="elementName">The element name to report in exceptions.</param>
107
108
/// <returns>The enum value contained within the JSON value.</returns>
108
109
internal static TEnum ToEnumValue < TEnum > ( this JsonValue jsonValue , string elementName )
109
- where TEnum : struct
110
+ where TEnum : struct , Enum
110
111
{
111
112
if ( ! jsonValue . IsString )
112
113
{
113
114
throw new InvalidSettingsException ( $ "{ elementName } must contain an enum (string) value") ;
114
115
}
115
116
116
117
TEnum result ;
117
- if ( ! Enum . TryParse ( jsonValue . AsString , true , out result ) )
118
+ if ( ! EnumHelper < TEnum > . TryParse ( jsonValue . AsString , out result ) )
118
119
{
119
120
throw new InvalidSettingsException ( $ "{ elementName } cannot contain enum value '{ jsonValue . AsString } '") ;
120
121
}
@@ -145,5 +146,25 @@ internal static void AssertIsObject(this KeyValuePair<string, JsonValue> jsonVal
145
146
throw new InvalidSettingsException ( $ "{ jsonValue . Key } must contain an object") ;
146
147
}
147
148
}
149
+
150
+ private static class EnumHelper < TEnum >
151
+ where TEnum : struct , Enum
152
+ {
153
+ private static ImmutableDictionary < string , KeyValuePair < bool , TEnum > > values = ImmutableDictionary < string , KeyValuePair < bool , TEnum > > . Empty ;
154
+
155
+ public static bool TryParse ( string value , out TEnum result )
156
+ {
157
+ var successAndResult = ImmutableInterlocked . GetOrAdd (
158
+ ref values ,
159
+ value ,
160
+ static value =>
161
+ {
162
+ bool success = Enum . TryParse ( value , true , out TEnum result ) ;
163
+ return new KeyValuePair < bool , TEnum > ( success , result ) ;
164
+ } ) ;
165
+ result = successAndResult . Value ;
166
+ return successAndResult . Key ;
167
+ }
168
+ }
148
169
}
149
170
}
0 commit comments