| | | 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.Collections.Generic; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Reflection; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Threading; |
| | | 9 | | |
| | | 10 | | namespace System.Text.Json.Serialization.Metadata |
| | | 11 | | { |
| | | 12 | | // Adapts the user-declared 'bool TryGetValue(out TCase)' instance methods on a union type |
| | | 13 | | // to a uniform '(TUnion, out Type?, out object?) => bool' shape: a single chained delegate |
| | | 14 | | // tries each TryGetValue overload in caller-supplied order and returns the matching case |
| | | 15 | | // type alongside the boxed value on first hit. Implementations live alongside the other |
| | | 16 | | // MemberAccessor helpers. |
| | | 17 | | internal delegate bool UnionTryGetValueAccessor<TUnion>(TUnion union, out Type? caseType, out object? value); |
| | | 18 | | |
| | | 19 | | internal abstract class MemberAccessor |
| | | 20 | | { |
| | | 21 | | private static MemberAccessor? s_instance; |
| | | 22 | | |
| | | 23 | | internal static MemberAccessor Instance |
| | | 24 | | { |
| | | 25 | | [RequiresUnreferencedCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 26 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 27 | | get |
| | 10432 | 28 | | { |
| | 10432 | 29 | | return s_instance ?? Initialize(); |
| | | 30 | | static MemberAccessor Initialize() |
| | 1 | 31 | | { |
| | 1 | 32 | | MemberAccessor value = |
| | 1 | 33 | | #if NET |
| | 1 | 34 | | // if dynamic code isn't supported, fallback to reflection |
| | 1 | 35 | | RuntimeFeature.IsDynamicCodeSupported ? |
| | 1 | 36 | | new ReflectionEmitCachingMemberAccessor() : |
| | 1 | 37 | | new ReflectionMemberAccessor(); |
| | | 38 | | #elif NETFRAMEWORK |
| | | 39 | | new ReflectionEmitCachingMemberAccessor(); |
| | | 40 | | #else |
| | | 41 | | new ReflectionMemberAccessor(); |
| | | 42 | | #endif |
| | 1 | 43 | | return Interlocked.CompareExchange(ref s_instance, value, null) ?? value; |
| | 1 | 44 | | } |
| | 10432 | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | internal static void ClearCache() => s_instance?.Clear(); |
| | | 49 | | |
| | | 50 | | public abstract Func<object>? CreateParameterlessConstructor(Type type, ConstructorInfo? constructorInfo); |
| | | 51 | | |
| | | 52 | | public abstract Func<object?[], T> CreateParameterizedConstructor<T>(ConstructorInfo constructor); |
| | | 53 | | |
| | | 54 | | public abstract JsonTypeInfo.ParameterizedConstructorDelegate<T, TArg0, TArg1, TArg2, TArg3>? CreateParameterize |
| | | 55 | | |
| | | 56 | | public abstract Func<object?, T> CreateSingleParameterConstructor<T>(ConstructorInfo constructor); |
| | | 57 | | |
| | | 58 | | public abstract Action<TCollection, object?> CreateAddMethodDelegate<[DynamicallyAccessedMembers(DynamicallyAcce |
| | | 59 | | |
| | | 60 | | public abstract Func<IEnumerable<TElement>, TCollection> CreateImmutableEnumerableCreateRangeDelegate<TCollectio |
| | | 61 | | |
| | | 62 | | public abstract Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection> CreateImmutableDictionaryCreateRangeD |
| | | 63 | | |
| | | 64 | | public abstract Func<object, TProperty> CreatePropertyGetter<TProperty>(PropertyInfo propertyInfo); |
| | | 65 | | |
| | | 66 | | public abstract Func<TDeclaringType, TProperty> CreatePropertyGetter<TDeclaringType, TProperty>(PropertyInfo pro |
| | | 67 | | |
| | | 68 | | public abstract Action<object, TProperty> CreatePropertySetter<TProperty>(PropertyInfo propertyInfo); |
| | | 69 | | |
| | | 70 | | public abstract Func<object, TProperty> CreateFieldGetter<TProperty>(FieldInfo fieldInfo); |
| | | 71 | | |
| | | 72 | | public abstract Action<object, TProperty> CreateFieldSetter<TProperty>(FieldInfo fieldInfo); |
| | | 73 | | |
| | | 74 | | public abstract UnionTryGetValueAccessor<TUnion> CreateUnionTryGetValueAccessor<TUnion>(IReadOnlyList<KeyValuePa |
| | | 75 | | |
| | 0 | 76 | | public virtual void Clear() { } |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |