| | | 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.Reflection; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | #if !BUILDING_SOURCE_GENERATOR |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | #endif |
| | | 10 | | |
| | | 11 | | namespace System.Text.Json.Reflection |
| | | 12 | | { |
| | | 13 | | internal static partial class ReflectionExtensions |
| | | 14 | | { |
| | | 15 | | // Immutable collection types. |
| | | 16 | | private const string ImmutableArrayGenericTypeName = "System.Collections.Immutable.ImmutableArray`1"; |
| | | 17 | | private const string ImmutableListGenericTypeName = "System.Collections.Immutable.ImmutableList`1"; |
| | | 18 | | private const string ImmutableListGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableList`1"; |
| | | 19 | | private const string ImmutableStackGenericTypeName = "System.Collections.Immutable.ImmutableStack`1"; |
| | | 20 | | private const string ImmutableStackGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableStack`1"; |
| | | 21 | | private const string ImmutableQueueGenericTypeName = "System.Collections.Immutable.ImmutableQueue`1"; |
| | | 22 | | private const string ImmutableQueueGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableQueue`1"; |
| | | 23 | | private const string ImmutableSortedSetGenericTypeName = "System.Collections.Immutable.ImmutableSortedSet`1"; |
| | | 24 | | private const string ImmutableHashSetGenericTypeName = "System.Collections.Immutable.ImmutableHashSet`1"; |
| | | 25 | | private const string ImmutableSetGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableSet`1"; |
| | | 26 | | private const string ImmutableDictionaryGenericTypeName = "System.Collections.Immutable.ImmutableDictionary`2"; |
| | | 27 | | private const string ImmutableDictionaryGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableDicti |
| | | 28 | | private const string ImmutableSortedDictionaryGenericTypeName = "System.Collections.Immutable.ImmutableSortedDic |
| | | 29 | | |
| | | 30 | | // Immutable collection builder types. |
| | | 31 | | private const string ImmutableArrayTypeName = "System.Collections.Immutable.ImmutableArray"; |
| | | 32 | | private const string ImmutableListTypeName = "System.Collections.Immutable.ImmutableList"; |
| | | 33 | | private const string ImmutableStackTypeName = "System.Collections.Immutable.ImmutableStack"; |
| | | 34 | | private const string ImmutableQueueTypeName = "System.Collections.Immutable.ImmutableQueue"; |
| | | 35 | | private const string ImmutableSortedSetTypeName = "System.Collections.Immutable.ImmutableSortedSet"; |
| | | 36 | | private const string ImmutableHashSetTypeName = "System.Collections.Immutable.ImmutableHashSet"; |
| | | 37 | | private const string ImmutableDictionaryTypeName = "System.Collections.Immutable.ImmutableDictionary"; |
| | | 38 | | private const string ImmutableSortedDictionaryTypeName = "System.Collections.Immutable.ImmutableSortedDictionary |
| | | 39 | | |
| | | 40 | | public const string CreateRangeMethodName = "CreateRange"; |
| | | 41 | | |
| | | 42 | | public static Type? GetCompatibleGenericBaseClass(this Type type, Type? baseType) |
| | 1366 | 43 | | { |
| | 1366 | 44 | | if (baseType is null) |
| | 0 | 45 | | { |
| | 0 | 46 | | return null; |
| | | 47 | | } |
| | | 48 | | |
| | 1366 | 49 | | Debug.Assert(baseType.IsGenericType); |
| | 1366 | 50 | | Debug.Assert(!baseType.IsInterface); |
| | 1366 | 51 | | Debug.Assert(baseType == baseType.GetGenericTypeDefinition()); |
| | | 52 | | |
| | 1366 | 53 | | Type? baseTypeToCheck = type; |
| | | 54 | | |
| | 1730 | 55 | | while (baseTypeToCheck != null && baseTypeToCheck != typeof(object)) |
| | 1366 | 56 | | { |
| | 1366 | 57 | | if (baseTypeToCheck.IsGenericType) |
| | 1366 | 58 | | { |
| | 1366 | 59 | | Type genericTypeToCheck = baseTypeToCheck.GetGenericTypeDefinition(); |
| | 1366 | 60 | | if (genericTypeToCheck == baseType) |
| | 1002 | 61 | | { |
| | 1002 | 62 | | return baseTypeToCheck; |
| | | 63 | | } |
| | 364 | 64 | | } |
| | | 65 | | |
| | 364 | 66 | | baseTypeToCheck = baseTypeToCheck.BaseType; |
| | 364 | 67 | | } |
| | | 68 | | |
| | 364 | 69 | | return null; |
| | 1366 | 70 | | } |
| | | 71 | | |
| | | 72 | | #if !BUILDING_SOURCE_GENERATOR |
| | | 73 | | [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", |
| | | 74 | | Justification = "The 'interfaceType' must exist and so trimmer kept it. In which case " + |
| | | 75 | | "It also kept it on any type which implements it. The below call to GetInterfaces " + |
| | | 76 | | "may return fewer results when trimmed but it will return the 'interfaceType' " + |
| | | 77 | | "if the type implemented it, even after trimming.")] |
| | | 78 | | #endif |
| | | 79 | | public static Type? GetCompatibleGenericInterface(this Type type, Type? interfaceType) |
| | 2301 | 80 | | { |
| | 2301 | 81 | | if (interfaceType is null) |
| | 0 | 82 | | { |
| | 0 | 83 | | return null; |
| | | 84 | | } |
| | | 85 | | |
| | 2301 | 86 | | Debug.Assert(interfaceType.IsGenericType); |
| | 2301 | 87 | | Debug.Assert(interfaceType.IsInterface); |
| | 2301 | 88 | | Debug.Assert(interfaceType == interfaceType.GetGenericTypeDefinition()); |
| | | 89 | | |
| | 2301 | 90 | | Type interfaceToCheck = type; |
| | | 91 | | |
| | 2301 | 92 | | if (interfaceToCheck.IsGenericType) |
| | 2301 | 93 | | { |
| | 2301 | 94 | | interfaceToCheck = interfaceToCheck.GetGenericTypeDefinition(); |
| | 2301 | 95 | | } |
| | | 96 | | |
| | 2301 | 97 | | if (interfaceToCheck == interfaceType) |
| | 0 | 98 | | { |
| | 0 | 99 | | return type; |
| | | 100 | | } |
| | | 101 | | |
| | 25889 | 102 | | foreach (Type typeToCheck in type.GetInterfaces()) |
| | 9493 | 103 | | { |
| | 9493 | 104 | | if (typeToCheck.IsGenericType) |
| | 5759 | 105 | | { |
| | 5759 | 106 | | Type genericInterfaceToCheck = typeToCheck.GetGenericTypeDefinition(); |
| | 5759 | 107 | | if (genericInterfaceToCheck == interfaceType) |
| | 0 | 108 | | { |
| | 0 | 109 | | return typeToCheck; |
| | | 110 | | } |
| | 5759 | 111 | | } |
| | 9493 | 112 | | } |
| | | 113 | | |
| | 2301 | 114 | | return null; |
| | 2301 | 115 | | } |
| | | 116 | | |
| | | 117 | | public static bool IsImmutableDictionaryType(this Type type) |
| | 0 | 118 | | { |
| | 0 | 119 | | if (!type.IsGenericType || !type.Assembly.FullName!.StartsWith("System.Collections.Immutable", StringCompari |
| | 0 | 120 | | { |
| | 0 | 121 | | return false; |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | switch (GetBaseNameFromGenericType(type)) |
| | | 125 | | { |
| | | 126 | | case ImmutableDictionaryGenericTypeName: |
| | | 127 | | case ImmutableDictionaryGenericInterfaceTypeName: |
| | | 128 | | case ImmutableSortedDictionaryGenericTypeName: |
| | 0 | 129 | | return true; |
| | | 130 | | default: |
| | 0 | 131 | | return false; |
| | | 132 | | } |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | public static bool IsImmutableEnumerableType(this Type type) |
| | 0 | 136 | | { |
| | 0 | 137 | | if (!type.IsGenericType || !type.Assembly.FullName!.StartsWith("System.Collections.Immutable", StringCompari |
| | 0 | 138 | | { |
| | 0 | 139 | | return false; |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | switch (GetBaseNameFromGenericType(type)) |
| | | 143 | | { |
| | | 144 | | case ImmutableArrayGenericTypeName: |
| | | 145 | | case ImmutableListGenericTypeName: |
| | | 146 | | case ImmutableListGenericInterfaceTypeName: |
| | | 147 | | case ImmutableStackGenericTypeName: |
| | | 148 | | case ImmutableStackGenericInterfaceTypeName: |
| | | 149 | | case ImmutableQueueGenericTypeName: |
| | | 150 | | case ImmutableQueueGenericInterfaceTypeName: |
| | | 151 | | case ImmutableSortedSetGenericTypeName: |
| | | 152 | | case ImmutableHashSetGenericTypeName: |
| | | 153 | | case ImmutableSetGenericInterfaceTypeName: |
| | 0 | 154 | | return true; |
| | | 155 | | default: |
| | 0 | 156 | | return false; |
| | | 157 | | } |
| | 0 | 158 | | } |
| | | 159 | | |
| | | 160 | | public static string? GetImmutableDictionaryConstructingTypeName(this Type type) |
| | 0 | 161 | | { |
| | 0 | 162 | | Debug.Assert(type.IsImmutableDictionaryType()); |
| | | 163 | | |
| | | 164 | | // Use the generic type definition of the immutable collection to determine |
| | | 165 | | // an appropriate constructing type, i.e. a type that we can invoke the |
| | | 166 | | // `CreateRange<T>` method on, which returns the desired immutable collection. |
| | 0 | 167 | | switch (GetBaseNameFromGenericType(type)) |
| | | 168 | | { |
| | | 169 | | case ImmutableDictionaryGenericTypeName: |
| | | 170 | | case ImmutableDictionaryGenericInterfaceTypeName: |
| | 0 | 171 | | return ImmutableDictionaryTypeName; |
| | | 172 | | case ImmutableSortedDictionaryGenericTypeName: |
| | 0 | 173 | | return ImmutableSortedDictionaryTypeName; |
| | | 174 | | default: |
| | | 175 | | // We verified that the type is an immutable collection, so the |
| | | 176 | | // generic definition is one of the above. |
| | 0 | 177 | | return null; |
| | | 178 | | } |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | public static string? GetImmutableEnumerableConstructingTypeName(this Type type) |
| | 0 | 182 | | { |
| | 0 | 183 | | Debug.Assert(type.IsImmutableEnumerableType()); |
| | | 184 | | |
| | | 185 | | // Use the generic type definition of the immutable collection to determine |
| | | 186 | | // an appropriate constructing type, i.e. a type that we can invoke the |
| | | 187 | | // `CreateRange<T>` method on, which returns the desired immutable collection. |
| | 0 | 188 | | switch (GetBaseNameFromGenericType(type)) |
| | | 189 | | { |
| | | 190 | | case ImmutableArrayGenericTypeName: |
| | 0 | 191 | | return ImmutableArrayTypeName; |
| | | 192 | | case ImmutableListGenericTypeName: |
| | | 193 | | case ImmutableListGenericInterfaceTypeName: |
| | 0 | 194 | | return ImmutableListTypeName; |
| | | 195 | | case ImmutableStackGenericTypeName: |
| | | 196 | | case ImmutableStackGenericInterfaceTypeName: |
| | 0 | 197 | | return ImmutableStackTypeName; |
| | | 198 | | case ImmutableQueueGenericTypeName: |
| | | 199 | | case ImmutableQueueGenericInterfaceTypeName: |
| | 0 | 200 | | return ImmutableQueueTypeName; |
| | | 201 | | case ImmutableSortedSetGenericTypeName: |
| | 0 | 202 | | return ImmutableSortedSetTypeName; |
| | | 203 | | case ImmutableHashSetGenericTypeName: |
| | | 204 | | case ImmutableSetGenericInterfaceTypeName: |
| | 0 | 205 | | return ImmutableHashSetTypeName; |
| | | 206 | | default: |
| | | 207 | | // We verified that the type is an immutable collection, so the |
| | | 208 | | // generic definition is one of the above. |
| | 0 | 209 | | return null; |
| | | 210 | | } |
| | 0 | 211 | | } |
| | | 212 | | |
| | | 213 | | private static string GetBaseNameFromGenericType(Type genericType) |
| | 0 | 214 | | { |
| | 0 | 215 | | Type genericTypeDef = genericType.GetGenericTypeDefinition(); |
| | 0 | 216 | | return genericTypeDef.FullName!; |
| | 0 | 217 | | } |
| | | 218 | | |
| | | 219 | | public static bool IsVirtual(this PropertyInfo propertyInfo) |
| | 16337 | 220 | | { |
| | 16337 | 221 | | return propertyInfo.GetMethod?.IsVirtual == true || propertyInfo.SetMethod?.IsVirtual == true; |
| | 16337 | 222 | | } |
| | | 223 | | |
| | | 224 | | public static bool IsKeyValuePair(this Type type) |
| | 1299 | 225 | | => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>); |
| | | 226 | | |
| | | 227 | | public static bool TryGetDeserializationConstructor( |
| | | 228 | | [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTyp |
| | | 229 | | this Type type, |
| | | 230 | | bool useDefaultCtorInAnnotatedStructs, |
| | | 231 | | out ConstructorInfo? deserializationCtor) |
| | 1299 | 232 | | { |
| | 1299 | 233 | | ConstructorInfo? ctorWithAttribute = null; |
| | 1299 | 234 | | ConstructorInfo? publicParameterlessCtor = null; |
| | 1299 | 235 | | ConstructorInfo? lonePublicCtor = null; |
| | | 236 | | |
| | 1299 | 237 | | ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance); |
| | | 238 | | |
| | 1299 | 239 | | if (constructors.Length == 1) |
| | 1299 | 240 | | { |
| | 1299 | 241 | | lonePublicCtor = constructors[0]; |
| | 1299 | 242 | | } |
| | | 243 | | |
| | 6495 | 244 | | foreach (ConstructorInfo constructor in constructors) |
| | 1299 | 245 | | { |
| | 1299 | 246 | | if (HasJsonConstructorAttribute(constructor)) |
| | 0 | 247 | | { |
| | 0 | 248 | | if (ctorWithAttribute != null) |
| | 0 | 249 | | { |
| | 0 | 250 | | deserializationCtor = null; |
| | 0 | 251 | | return false; |
| | | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | ctorWithAttribute = constructor; |
| | 0 | 255 | | } |
| | 1299 | 256 | | else if (constructor.GetParameters().Length == 0) |
| | 550 | 257 | | { |
| | 550 | 258 | | publicParameterlessCtor = constructor; |
| | 550 | 259 | | } |
| | 1299 | 260 | | } |
| | | 261 | | |
| | | 262 | | // Search for non-public ctors with [JsonConstructor]. |
| | 5395 | 263 | | foreach (ConstructorInfo constructor in type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance) |
| | 749 | 264 | | { |
| | 749 | 265 | | if (HasJsonConstructorAttribute(constructor)) |
| | 0 | 266 | | { |
| | 0 | 267 | | if (ctorWithAttribute != null) |
| | 0 | 268 | | { |
| | 0 | 269 | | deserializationCtor = null; |
| | 0 | 270 | | return false; |
| | | 271 | | } |
| | | 272 | | |
| | 0 | 273 | | ctorWithAttribute = constructor; |
| | 0 | 274 | | } |
| | 749 | 275 | | } |
| | | 276 | | |
| | | 277 | | // Structs will use default constructor if attribute isn't used. |
| | 1299 | 278 | | if (useDefaultCtorInAnnotatedStructs && type.IsValueType && ctorWithAttribute == null) |
| | 0 | 279 | | { |
| | 0 | 280 | | deserializationCtor = null; |
| | 0 | 281 | | return true; |
| | | 282 | | } |
| | | 283 | | |
| | 1299 | 284 | | deserializationCtor = ctorWithAttribute ?? publicParameterlessCtor ?? lonePublicCtor; |
| | 1299 | 285 | | return true; |
| | 1299 | 286 | | } |
| | | 287 | | |
| | | 288 | | public static object? GetDefaultValue(this ParameterInfo parameterInfo) |
| | 4494 | 289 | | { |
| | 4494 | 290 | | Type parameterType = parameterInfo.ParameterType; |
| | 4494 | 291 | | object? defaultValue = parameterInfo.DefaultValue; |
| | | 292 | | |
| | 4494 | 293 | | if (defaultValue is null) |
| | 0 | 294 | | { |
| | 0 | 295 | | return null; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | // DBNull.Value is sometimes used as the default value (returned by reflection) of nullable params in place |
| | 4494 | 299 | | if (defaultValue == DBNull.Value && parameterType != typeof(DBNull)) |
| | 4494 | 300 | | { |
| | 4494 | 301 | | return null; |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | // Default values of enums or nullable enums are represented using the underlying type and need to be cast e |
| | | 305 | | // cf. https://github.com/dotnet/runtime/issues/68647 |
| | 0 | 306 | | if (parameterType.IsEnum) |
| | 0 | 307 | | { |
| | 0 | 308 | | return Enum.ToObject(parameterType, defaultValue); |
| | | 309 | | } |
| | | 310 | | |
| | 0 | 311 | | if (Nullable.GetUnderlyingType(parameterType) is Type underlyingType && underlyingType.IsEnum) |
| | 0 | 312 | | { |
| | 0 | 313 | | return Enum.ToObject(underlyingType, defaultValue); |
| | | 314 | | } |
| | | 315 | | |
| | 0 | 316 | | return defaultValue; |
| | 4494 | 317 | | } |
| | | 318 | | |
| | | 319 | | /// <summary> |
| | | 320 | | /// Returns the type hierarchy for the given type, starting from the current type up to the base type(s) in the |
| | | 321 | | /// Interface hierarchies with multiple inheritance will return results using topological sorting. |
| | | 322 | | /// </summary> |
| | | 323 | | [RequiresUnreferencedCode("Should only be used by the reflection-based serializer.")] |
| | | 324 | | public static Type[] GetSortedTypeHierarchy(this Type type) |
| | 1299 | 325 | | { |
| | 1299 | 326 | | if (!type.IsInterface) |
| | 1299 | 327 | | { |
| | | 328 | | // Non-interface hierarchies are linear, just walk up to the earliest ancestor. |
| | | 329 | | |
| | 1299 | 330 | | var results = new List<Type>(); |
| | 7794 | 331 | | for (Type? current = type; current != null; current = current.BaseType) |
| | 2598 | 332 | | { |
| | 2598 | 333 | | results.Add(current); |
| | 2598 | 334 | | } |
| | | 335 | | |
| | 1299 | 336 | | return results.ToArray(); |
| | | 337 | | } |
| | | 338 | | else |
| | 0 | 339 | | { |
| | | 340 | | // Interface hierarchies support multiple inheritance. |
| | | 341 | | // For consistency with class hierarchy resolution order, |
| | | 342 | | // sort topologically from most derived to least derived. |
| | 0 | 343 | | return JsonHelpers.TraverseGraphWithTopologicalSort(type, static t => t.GetInterfaces()); |
| | | 344 | | } |
| | 1299 | 345 | | } |
| | | 346 | | } |
| | | 347 | | } |