@@ -35,7 +35,7 @@ public class SessionStateTempDataProvider : ITempDataProvider
35
35
36
36
private static readonly ConcurrentDictionary < Type , Func < JArray , object > > _arrayConverters =
37
37
new ConcurrentDictionary < Type , Func < JArray , object > > ( ) ;
38
- private static readonly ConcurrentDictionary < Type , Func < JObject , object > > _dictConverters =
38
+ private static readonly ConcurrentDictionary < Type , Func < JObject , object > > _dictionaryConverters =
39
39
new ConcurrentDictionary < Type , Func < JObject , object > > ( ) ;
40
40
41
41
private static readonly Dictionary < JTokenType , Type > _tokenTypeLookup = new Dictionary < JTokenType , Type >
@@ -77,8 +77,9 @@ public virtual IDictionary<string, object> LoadTempData([NotNull] HttpContext co
77
77
var jArrayValue = item . Value as JArray ;
78
78
if ( jArrayValue != null && jArrayValue . Count > 0 )
79
79
{
80
+ var arrayType = jArrayValue [ 0 ] . Type ;
80
81
Type returnType ;
81
- if ( _tokenTypeLookup . TryGetValue ( jArrayValue [ 0 ] . Type , out returnType ) )
82
+ if ( _tokenTypeLookup . TryGetValue ( arrayType , out returnType ) )
82
83
{
83
84
var arrayConverter = _arrayConverters . GetOrAdd ( returnType , type =>
84
85
{
@@ -90,7 +91,7 @@ public virtual IDictionary<string, object> LoadTempData([NotNull] HttpContext co
90
91
}
91
92
else
92
93
{
93
- var message = Resources . FormatTempData_CannotDeserializeToken ( jArrayValue [ 0 ] . Type ) ;
94
+ var message = Resources . FormatTempData_CannotDeserializeToken ( arrayType ) ;
94
95
throw new InvalidOperationException ( message ) ;
95
96
}
96
97
}
@@ -106,7 +107,7 @@ public virtual IDictionary<string, object> LoadTempData([NotNull] HttpContext co
106
107
Type valueType ;
107
108
if ( _tokenTypeLookup . TryGetValue ( jTokenType , out valueType ) )
108
109
{
109
- var dictConverter = _dictConverters . GetOrAdd ( valueType , type =>
110
+ var dictConverter = _dictionaryConverters . GetOrAdd ( valueType , type =>
110
111
{
111
112
return ( Func < JObject , object > ) _convertDictMethodInfo . MakeGenericMethod ( type ) . CreateDelegate ( typeof ( Func < JObject , object > ) ) ;
112
113
} ) ;
@@ -174,7 +175,7 @@ private static bool IsSessionEnabled(HttpContext context)
174
175
internal void EnsureObjectCanBeSerialized ( object item )
175
176
{
176
177
var itemType = item . GetType ( ) ;
177
- Type [ ] actualTypes = null ;
178
+ Type actualType = null ;
178
179
179
180
if ( itemType . IsArray )
180
181
{
@@ -185,29 +186,35 @@ internal void EnsureObjectCanBeSerialized(object item)
185
186
if ( itemType . ExtractGenericInterface ( typeof ( IList < > ) ) != null ||
186
187
itemType . ExtractGenericInterface ( typeof ( IDictionary < , > ) ) != null )
187
188
{
188
- actualTypes = itemType . GetGenericArguments ( ) ;
189
+ var genericTypeArguments = itemType . GetGenericArguments ( ) ;
190
+ if ( genericTypeArguments . Length > 1 )
191
+ {
192
+ // Throw if the key type of the dictionary is not string.
193
+ if ( genericTypeArguments [ 0 ] != typeof ( string ) )
194
+ {
195
+ var message = Resources . FormatTempData_CannotSerializeDictionary ( genericTypeArguments [ 0 ] ,
196
+ typeof ( SessionStateTempDataProvider ) . FullName ) ;
197
+ throw new InvalidOperationException ( message ) ;
198
+ }
199
+ else
200
+ {
201
+ actualType = genericTypeArguments [ 1 ] ;
202
+ }
203
+ }
204
+ else
205
+ {
206
+ actualType = genericTypeArguments [ 0 ] ;
207
+ }
189
208
}
190
209
}
191
210
192
- actualTypes = actualTypes ?? new Type [ ] { itemType } ;
193
-
194
- // Throw if the key type of the dictionary is not string.
195
- if ( actualTypes . Length > 1 && actualTypes [ 0 ] != typeof ( string ) )
196
- {
197
- var message = Resources . FormatTempData_CannotSerializeDictionary ( actualTypes [ 0 ] ,
198
- typeof ( SessionStateTempDataProvider ) . FullName ) ;
199
- throw new InvalidOperationException ( message ) ;
200
- }
201
-
202
- foreach ( var actualType in actualTypes )
211
+ actualType = actualType ?? itemType ;
212
+ if ( ! TypeHelper . IsSimpleType ( actualType ) )
203
213
{
204
214
var underlyingType = Nullable . GetUnderlyingType ( actualType ) ?? actualType ;
205
- if ( ! TypeHelper . IsSimpleType ( actualType ) )
206
- {
207
- var message = Resources . FormatTempData_CannotSerializeToSession ( underlyingType ,
208
- typeof ( SessionStateTempDataProvider ) . FullName ) ;
209
- throw new InvalidOperationException ( message ) ;
210
- }
215
+ var message = Resources . FormatTempData_CannotSerializeToSession ( underlyingType ,
216
+ typeof ( SessionStateTempDataProvider ) . FullName ) ;
217
+ throw new InvalidOperationException ( message ) ;
211
218
}
212
219
}
213
220
@@ -216,12 +223,12 @@ private static IList<TVal> ConvertArray<TVal>(JArray array)
216
223
return array . Values < TVal > ( ) . ToArray ( ) ;
217
224
}
218
225
219
- private static IDictionary < string , T > ConvertDict < T > ( JObject jObject )
226
+ private static IDictionary < string , TVal > ConvertDict < TVal > ( JObject jObject )
220
227
{
221
- var convertedDict = new Dictionary < string , T > ( ) ;
228
+ var convertedDict = new Dictionary < string , TVal > ( StringComparer . Ordinal ) ;
222
229
foreach ( var item in jObject )
223
230
{
224
- convertedDict . Add ( item . Key , jObject . Value < T > ( item . Key ) ) ;
231
+ convertedDict . Add ( item . Key , jObject . Value < TVal > ( item . Key ) ) ;
225
232
}
226
233
return convertedDict ;
227
234
}
0 commit comments