Skip to content

Commit a81d042

Browse files
committed
simplify checks
1 parent d4fbe9a commit a81d042

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/Services/SessionState/JsonSessionKeySerializer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ public bool TrySerialize(string key, object? value, out byte[] bytes)
5151
{
5252
if (value is null)
5353
{
54-
if (!type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)))
54+
if (!type.IsValueType || IsNullable(type))
5555
{
5656
// Create a new one instead of caching since technically the array values could be overwritten
5757
bytes = "null"u8.ToArray();
5858
return true;
5959
}
6060
}
61-
else if (type == value.GetType() || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) && type.GenericTypeArguments[0] == value.GetType()))
61+
else if (type == value.GetType() || IsNullableType(type, value.GetType()))
6262
{
6363
try
6464
{
@@ -79,5 +79,11 @@ public bool TrySerialize(string key, object? value, out byte[] bytes)
7979
bytes = Array.Empty<byte>();
8080
return false;
8181
}
82+
83+
private static bool IsNullable(Type type)
84+
=> type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
85+
86+
private static bool IsNullableType(Type type, Type nullableArg)
87+
=> IsNullable(type) && nullableArg == type.GenericTypeArguments[0];
8288
}
8389

0 commit comments

Comments
 (0)