<PackageReference Include="System.Text.Json" Version="6.0.0-preview.2.21154.6" />

ObjectConverterFactory

using System.Collections.Generic; using System.Reflection; namespace System.Text.Json.Serialization.Converters { internal class ObjectConverterFactory : JsonConverterFactory { public override bool CanConvert(Type typeToConvert) { return true; } public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) { if (IsKeyValuePair(typeToConvert)) return CreateKeyValuePairConverter(typeToConvert, options); ConstructorInfo deserializationConstructor = GetDeserializationConstructor(typeToConvert); ParameterInfo[] array = deserializationConstructor?.GetParameters(); Type type; if (deserializationConstructor == (ConstructorInfo)null || typeToConvert.IsAbstract || array.Length == 0) type = typeof(ObjectDefaultConverter<>).MakeGenericType(typeToConvert); else { int num = array.Length; if (num <= 4) { Type objectType = JsonClassInfo.ObjectType; Type[] array2 = new Type[5] { typeToConvert, null, null, null, null }; for (int i = 0; i < 4; i++) { if (i < num) array2[i + 1] = array[i].ParameterType; else array2[i + 1] = objectType; } type = typeof(SmallObjectWithParameterizedConstructorConverter<, , , , >).MakeGenericType(array2); } else type = typeof(LargeObjectWithParameterizedConstructorConverter<>).MakeGenericType(typeToConvert); } JsonConverter jsonConverter = (JsonConverter)Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public, null, null, null); jsonConverter.ConstructorInfo = deserializationConstructor; return jsonConverter; } private bool IsKeyValuePair(Type typeToConvert) { if (!typeToConvert.IsGenericType) return false; Type genericTypeDefinition = typeToConvert.GetGenericTypeDefinition(); return genericTypeDefinition == typeof(KeyValuePair<, >); } private JsonConverter CreateKeyValuePairConverter(Type type, JsonSerializerOptions options) { Type type2 = type.GetGenericArguments()[0]; Type type3 = type.GetGenericArguments()[1]; JsonConverter jsonConverter = (JsonConverter)Activator.CreateInstance(typeof(KeyValuePairConverter<, >).MakeGenericType(type2, type3), BindingFlags.Instance | BindingFlags.Public, null, null, null); jsonConverter.Initialize(options); return jsonConverter; } private ConstructorInfo GetDeserializationConstructor(Type type) { ConstructorInfo constructorInfo = null; ConstructorInfo constructorInfo2 = null; ConstructorInfo constructorInfo3 = null; ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public); if (constructors.Length == 1) constructorInfo3 = constructors[0]; ConstructorInfo[] array = constructors; foreach (ConstructorInfo constructorInfo4 in array) { if (constructorInfo4.GetCustomAttribute<JsonConstructorAttribute>() != null) { if (constructorInfo != (ConstructorInfo)null) ThrowHelper.ThrowInvalidOperationException_SerializationDuplicateTypeAttribute<JsonConstructorAttribute>(type); constructorInfo = constructorInfo4; } else if (constructorInfo4.GetParameters().Length == 0) { constructorInfo2 = constructorInfo4; } } ConstructorInfo left = constructorInfo; constructors = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic); ConstructorInfo[] array2 = constructors; foreach (ConstructorInfo constructorInfo5 in array2) { if (constructorInfo5.GetCustomAttribute<JsonConstructorAttribute>() != null) { if (left != (ConstructorInfo)null) ThrowHelper.ThrowInvalidOperationException_SerializationDuplicateTypeAttribute<JsonConstructorAttribute>(type); left = constructorInfo5; } } if (type.IsValueType && constructorInfo == (ConstructorInfo)null) return null; return constructorInfo ?? constructorInfo2 ?? constructorInfo3; } } }