| | | 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 | | #if NETFRAMEWORK || NET |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Reflection; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization.Metadata |
| | | 10 | | { |
| | | 11 | | internal sealed partial class ReflectionEmitCachingMemberAccessor : MemberAccessor |
| | | 12 | | { |
| | | 13 | | private readonly ReflectionEmitMemberAccessor _sourceAccessor; |
| | | 14 | | private readonly Cache<(string id, Type declaringType, MemberInfo? member)> _cache; |
| | | 15 | | |
| | | 16 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 17 | | [RequiresUnreferencedCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | 1 | 18 | | public ReflectionEmitCachingMemberAccessor() |
| | 1 | 19 | | { |
| | 1 | 20 | | _sourceAccessor = new ReflectionEmitMemberAccessor(); |
| | 1 | 21 | | _cache = new(slidingExpiration: TimeSpan.FromMilliseconds(1000), evictionInterval: TimeSpan.FromMilliseconds |
| | 1 | 22 | | } |
| | | 23 | | |
| | 0 | 24 | | public override void Clear() => _cache.Clear(); |
| | | 25 | | |
| | | 26 | | public override Action<TCollection, object?> CreateAddMethodDelegate<[DynamicallyAccessedMembers(DynamicallyAcce |
| | 0 | 27 | | _cache.GetOrAdd( |
| | 0 | 28 | | key: (nameof(CreateAddMethodDelegate), typeof(TCollection), null), |
| | 0 | 29 | | _ => _sourceAccessor.CreateAddMethodDelegate<TCollection>()); |
| | | 30 | | |
| | | 31 | | public override Func<object>? CreateParameterlessConstructor(Type type, ConstructorInfo? ctorInfo) => |
| | 12254 | 32 | | _cache.GetOrAdd( |
| | 12254 | 33 | | key: (nameof(CreateParameterlessConstructor), type, ctorInfo), |
| | 15594 | 34 | | valueFactory: key => _sourceAccessor.CreateParameterlessConstructor(key.declaringType, (ConstructorInfo? |
| | | 35 | | |
| | | 36 | | public override Func<object, TProperty> CreateFieldGetter<TProperty>(FieldInfo fieldInfo) => |
| | 0 | 37 | | _cache.GetOrAdd( |
| | 0 | 38 | | key: (nameof(CreateFieldGetter), typeof(TProperty), fieldInfo), |
| | 0 | 39 | | valueFactory: key => _sourceAccessor.CreateFieldGetter<TProperty>((FieldInfo)key.member!)); |
| | | 40 | | |
| | | 41 | | public override Action<object, TProperty> CreateFieldSetter<TProperty>(FieldInfo fieldInfo) => |
| | 0 | 42 | | _cache.GetOrAdd( |
| | 0 | 43 | | key: (nameof(CreateFieldSetter), typeof(TProperty), fieldInfo), |
| | 0 | 44 | | valueFactory: key => _sourceAccessor.CreateFieldSetter<TProperty>((FieldInfo)key.member!)); |
| | | 45 | | |
| | | 46 | | public override Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection> CreateImmutableDictionaryCreateRangeD |
| | 0 | 47 | | _cache.GetOrAdd( |
| | 0 | 48 | | key: (nameof(CreateImmutableDictionaryCreateRangeDelegate), typeof((TCollection, TKey, TValue)), null), |
| | 0 | 49 | | valueFactory: _ => _sourceAccessor.CreateImmutableDictionaryCreateRangeDelegate<TCollection, TKey, TValu |
| | | 50 | | |
| | | 51 | | public override Func<IEnumerable<TElement>, TCollection> CreateImmutableEnumerableCreateRangeDelegate<TCollectio |
| | 0 | 52 | | _cache.GetOrAdd( |
| | 0 | 53 | | key: (nameof(CreateImmutableEnumerableCreateRangeDelegate), typeof((TCollection, TElement)), null), |
| | 0 | 54 | | valueFactory: _ => _sourceAccessor.CreateImmutableEnumerableCreateRangeDelegate<TCollection, TElement>() |
| | | 55 | | |
| | | 56 | | public override Func<object[], T> CreateParameterizedConstructor<T>(ConstructorInfo constructor) => |
| | 749 | 57 | | _cache.GetOrAdd( |
| | 749 | 58 | | key: (nameof(CreateParameterizedConstructor), typeof(T), constructor), |
| | 1487 | 59 | | valueFactory: key => _sourceAccessor.CreateParameterizedConstructor<T>((ConstructorInfo)key.member!)); |
| | | 60 | | |
| | | 61 | | public override JsonTypeInfo.ParameterizedConstructorDelegate<T, TArg0, TArg1, TArg2, TArg3>? CreateParameterize |
| | 0 | 62 | | _cache.GetOrAdd( |
| | 0 | 63 | | key: (nameof(CreateParameterizedConstructor), typeof(T), constructor), |
| | 0 | 64 | | valueFactory: key => _sourceAccessor.CreateParameterizedConstructor<T, TArg0, TArg1, TArg2, TArg3>((Cons |
| | | 65 | | |
| | | 66 | | public override Func<object, TProperty> CreatePropertyGetter<TProperty>(PropertyInfo propertyInfo) => |
| | 7794 | 67 | | _cache.GetOrAdd( |
| | 7794 | 68 | | key: (nameof(CreatePropertyGetter), typeof(TProperty), propertyInfo), |
| | 15510 | 69 | | valueFactory: key => _sourceAccessor.CreatePropertyGetter<TProperty>((PropertyInfo)key.member!)); |
| | | 70 | | |
| | | 71 | | public override Action<object, TProperty> CreatePropertySetter<TProperty>(PropertyInfo propertyInfo) => |
| | 7794 | 72 | | _cache.GetOrAdd( |
| | 7794 | 73 | | key: (nameof(CreatePropertySetter), typeof(TProperty), propertyInfo), |
| | 15510 | 74 | | valueFactory: key => _sourceAccessor.CreatePropertySetter<TProperty>((PropertyInfo)key.member!)); |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | #endif |