| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Reflection; |
| | | 7 | | using System.Text.Json.Reflection; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization |
| | | 10 | | { |
| | | 11 | | internal static class IEnumerableConverterFactoryHelpers |
| | | 12 | | { |
| | | 13 | | // System.Text.Json doesn't take a direct reference to System.Collections.Immutable so |
| | | 14 | | // any netstandard2.0 consumers don't need to reference System.Collections.Immutable. |
| | | 15 | | // So instead, implement a "weak reference" by using strings to check for Immutable types. |
| | | 16 | | |
| | | 17 | | // Don't use DynamicDependency attributes to the Immutable Collection types so they can be trimmed in applicatio |
| | | 18 | | internal const string ImmutableConvertersUnreferencedCodeMessage = "System.Collections.Immutable converters use |
| | | 19 | | |
| | | 20 | | [RequiresUnreferencedCode(ImmutableConvertersUnreferencedCodeMessage)] |
| | | 21 | | [RequiresDynamicCode(ImmutableConvertersUnreferencedCodeMessage)] |
| | | 22 | | public static MethodInfo GetImmutableEnumerableCreateRangeMethod(this Type type, Type elementType) |
| | 0 | 23 | | { |
| | 0 | 24 | | Type? constructingType = GetImmutableEnumerableConstructingType(type); |
| | 0 | 25 | | if (constructingType != null) |
| | 0 | 26 | | { |
| | 0 | 27 | | MethodInfo[] constructingTypeMethods = constructingType.GetMethods(); |
| | 0 | 28 | | foreach (MethodInfo method in constructingTypeMethods) |
| | 0 | 29 | | { |
| | 0 | 30 | | if (method.Name == ReflectionExtensions.CreateRangeMethodName && |
| | 0 | 31 | | method.GetParameters().Length == 1 && |
| | 0 | 32 | | method.IsGenericMethod && |
| | 0 | 33 | | method.GetGenericArguments().Length == 1) |
| | 0 | 34 | | { |
| | 0 | 35 | | return method.MakeGenericMethod(elementType); |
| | | 36 | | } |
| | 0 | 37 | | } |
| | 0 | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(type); |
| | | 41 | | return null!; |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | [RequiresUnreferencedCode(ImmutableConvertersUnreferencedCodeMessage)] |
| | | 45 | | [RequiresDynamicCode(ImmutableConvertersUnreferencedCodeMessage)] |
| | | 46 | | public static MethodInfo GetImmutableDictionaryCreateRangeMethod(this Type type, Type keyType, Type valueType) |
| | 0 | 47 | | { |
| | 0 | 48 | | Type? constructingType = GetImmutableDictionaryConstructingType(type); |
| | 0 | 49 | | if (constructingType != null) |
| | 0 | 50 | | { |
| | 0 | 51 | | MethodInfo[] constructingTypeMethods = constructingType.GetMethods(); |
| | 0 | 52 | | foreach (MethodInfo method in constructingTypeMethods) |
| | 0 | 53 | | { |
| | 0 | 54 | | if (method.Name == ReflectionExtensions.CreateRangeMethodName && |
| | 0 | 55 | | method.GetParameters().Length == 1 && |
| | 0 | 56 | | method.IsGenericMethod && |
| | 0 | 57 | | method.GetGenericArguments().Length == 2) |
| | 0 | 58 | | { |
| | 0 | 59 | | return method.MakeGenericMethod(keyType, valueType); |
| | | 60 | | } |
| | 0 | 61 | | } |
| | 0 | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(type); |
| | | 65 | | return null!; |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | [RequiresUnreferencedCode(ImmutableConvertersUnreferencedCodeMessage)] |
| | | 69 | | [RequiresDynamicCode(ImmutableConvertersUnreferencedCodeMessage)] |
| | | 70 | | private static Type? GetImmutableEnumerableConstructingType(Type type) |
| | 0 | 71 | | { |
| | 0 | 72 | | Debug.Assert(type.IsImmutableEnumerableType()); |
| | | 73 | | |
| | 0 | 74 | | string? constructingTypeName = type.GetImmutableEnumerableConstructingTypeName(); |
| | | 75 | | |
| | 0 | 76 | | return constructingTypeName == null |
| | 0 | 77 | | ? null |
| | 0 | 78 | | : type.Assembly.GetType(constructingTypeName); |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | [RequiresUnreferencedCode(ImmutableConvertersUnreferencedCodeMessage)] |
| | | 82 | | [RequiresDynamicCode(ImmutableConvertersUnreferencedCodeMessage)] |
| | | 83 | | private static Type? GetImmutableDictionaryConstructingType(Type type) |
| | 0 | 84 | | { |
| | 0 | 85 | | Debug.Assert(type.IsImmutableDictionaryType()); |
| | | 86 | | |
| | 0 | 87 | | string? constructingTypeName = type.GetImmutableDictionaryConstructingTypeName(); |
| | | 88 | | |
| | 0 | 89 | | return constructingTypeName == null |
| | 0 | 90 | | ? null |
| | 0 | 91 | | : type.Assembly.GetType(constructingTypeName); |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | public static bool IsNonGenericStackOrQueue(this Type type) |
| | 0 | 95 | | { |
| | | 96 | | #if NET |
| | | 97 | | // Optimize for linking scenarios where mscorlib is trimmed out. |
| | | 98 | | const string stackTypeName = "System.Collections.Stack, System.Collections.NonGeneric"; |
| | | 99 | | const string queueTypeName = "System.Collections.Queue, System.Collections.NonGeneric"; |
| | | 100 | | #else |
| | | 101 | | const string stackTypeName = "System.Collections.Stack, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKe |
| | | 102 | | const string queueTypeName = "System.Collections.Queue, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKe |
| | | 103 | | #endif |
| | | 104 | | |
| | 0 | 105 | | Type? stackType = GetTypeIfExists(stackTypeName); |
| | 0 | 106 | | if (stackType?.IsAssignableFrom(type) == true) |
| | 0 | 107 | | { |
| | 0 | 108 | | return true; |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | Type? queueType = GetTypeIfExists(queueTypeName); |
| | 0 | 112 | | if (queueType?.IsAssignableFrom(type) == true) |
| | 0 | 113 | | { |
| | 0 | 114 | | return true; |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | return false; |
| | 0 | 118 | | } |
| | | 119 | | |
| | | 120 | | // This method takes an unannotated string which makes trimming reflection analysis lose track of the type we ar |
| | | 121 | | // looking for. This indirection allows the removal of the type if it is not used in the calling application. |
| | | 122 | | [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2057:TypeGetType", |
| | | 123 | | Justification = "This method exists to allow for 'weak references' to the Stack and Queue types. If those ty |
| | | 124 | | "they will be preserved by the app and Type.GetType will return them. If those types are not used in the app |
| | 0 | 125 | | private static Type? GetTypeIfExists(string name) => Type.GetType(name, false); |
| | | 126 | | } |
| | | 127 | | } |