< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 14
Coverable lines: 14
Total lines: 79
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
ClearCache()0%220%
Clear()100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/MemberAccessor.cs

#LineLine coverage
 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
 4using System.Collections.Generic;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Reflection;
 7using System.Runtime.CompilerServices;
 8using System.Threading;
 9
 10namespace 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
 028            {
 029                return s_instance ?? Initialize();
 30                static MemberAccessor Initialize()
 031                {
 032                    MemberAccessor value =
 033#if NET
 034                        // if dynamic code isn't supported, fallback to reflection
 035                        RuntimeFeature.IsDynamicCodeSupported ?
 036                            new ReflectionEmitCachingMemberAccessor() :
 037                            new ReflectionMemberAccessor();
 38#elif NETFRAMEWORK
 39                            new ReflectionEmitCachingMemberAccessor();
 40#else
 41                            new ReflectionMemberAccessor();
 42#endif
 043                    return Interlocked.CompareExchange(ref s_instance, value, null) ?? value;
 044                }
 045            }
 46        }
 47
 048        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
 076        public virtual void Clear() { }
 77    }
 78}
 79