return propertyInfo.GetMethod?.IsVirtual == true || propertyInfo.SetMethod?.IsVirtual == true;
}
- public static bool IsKeyValuePair(this Type type, Type? keyValuePairType = null)
- {
- if (!type.IsGenericType)
- {
- return false;
- }
-
- // Work around not being able to use typeof(KeyValuePair<,>) directly during compile-time src gen type analysis.
- keyValuePairType ??= typeof(KeyValuePair<,>);
-
- Type generic = type.GetGenericTypeDefinition();
- return generic == keyValuePairType;
- }
+ public static bool IsKeyValuePair(this Type type)
+ => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>);
public static bool TryGetDeserializationConstructor(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
+using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
Justification = "The ctor is marked RequiresUnreferencedCode.")]
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
+ ConstructorInfo? constructor;
JsonConverter converter;
Type converterType;
- bool useDefaultCtorInUnannotatedStructs = _useDefaultConstructorInUnannotatedStructs && !typeToConvert.IsKeyValuePair();
- if (!typeToConvert.TryGetDeserializationConstructor(useDefaultCtorInUnannotatedStructs, out ConstructorInfo? constructor))
+ if (typeToConvert.IsKeyValuePair())
+ {
+ // browser-wasm compat -- ensure the linker doesn't trim away constructor parameter names from KVP.
+ Type[] genericArguments = typeToConvert.GetGenericArguments();
+ Type keyValuePairType = typeof(KeyValuePair<,>).MakeGenericType(genericArguments);
+ constructor = keyValuePairType.GetConstructor(genericArguments);
+ }
+ else if (!typeToConvert.TryGetDeserializationConstructor(_useDefaultConstructorInUnannotatedStructs, out constructor))
{
ThrowHelper.ThrowInvalidOperationException_SerializationDuplicateTypeAttribute<JsonConstructorAttribute>(typeToConvert);
}