| | | 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; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Reflection; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization.Metadata |
| | | 10 | | { |
| | | 11 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 12 | | [RequiresUnreferencedCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 13 | | internal sealed class ReflectionMemberAccessor : MemberAccessor |
| | | 14 | | { |
| | 0 | 15 | | public ReflectionMemberAccessor() |
| | 0 | 16 | | { |
| | 0 | 17 | | } |
| | | 18 | | |
| | | 19 | | public override Func<object>? CreateParameterlessConstructor(Type type, ConstructorInfo? ctorInfo) |
| | 0 | 20 | | { |
| | 0 | 21 | | Debug.Assert(type != null); |
| | 0 | 22 | | Debug.Assert(ctorInfo is null || ctorInfo.GetParameters().Length == 0); |
| | | 23 | | |
| | 0 | 24 | | if (type.IsAbstract) |
| | 0 | 25 | | { |
| | 0 | 26 | | return null; |
| | | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | if (ctorInfo is null) |
| | 0 | 30 | | { |
| | 0 | 31 | | return type.IsValueType |
| | 0 | 32 | | ? () => Activator.CreateInstance(type, nonPublic: false)! |
| | 0 | 33 | | : null; |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | return () => ctorInfo.Invoke(null); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | public override Func<object[], T> CreateParameterizedConstructor<T>(ConstructorInfo constructor) |
| | 0 | 40 | | { |
| | 0 | 41 | | Type type = typeof(T); |
| | | 42 | | |
| | 0 | 43 | | Debug.Assert(!type.IsAbstract); |
| | 0 | 44 | | Debug.Assert(constructor.DeclaringType == type && constructor.IsPublic && !constructor.IsStatic); |
| | | 45 | | |
| | 0 | 46 | | int parameterCount = constructor.GetParameters().Length; |
| | | 47 | | |
| | 0 | 48 | | return (arguments) => |
| | 0 | 49 | | { |
| | 0 | 50 | | // The input array was rented from the shared ArrayPool, so its size is likely to be larger than the par |
| | 0 | 51 | | // The emit equivalent of this method does not (need to) allocate here + transfer the objects. |
| | 0 | 52 | | object[] argsToPass = new object[parameterCount]; |
| | 0 | 53 | | |
| | 0 | 54 | | for (int i = 0; i < parameterCount; i++) |
| | 0 | 55 | | { |
| | 0 | 56 | | argsToPass[i] = arguments[i]; |
| | 0 | 57 | | } |
| | 0 | 58 | | |
| | 0 | 59 | | try |
| | 0 | 60 | | { |
| | 0 | 61 | | return (T)constructor.Invoke(argsToPass); |
| | 0 | 62 | | } |
| | 0 | 63 | | catch (TargetInvocationException e) |
| | 0 | 64 | | { |
| | 0 | 65 | | // Plumb ArgumentException through for tuples with more than 7 generic parameters, e.g. |
| | 0 | 66 | | // System.ArgumentException : The last element of an eight element tuple must be a Tuple. |
| | 0 | 67 | | // This doesn't apply to the method below as it supports a max of 4 constructor params. |
| | 0 | 68 | | throw e.InnerException ?? e; |
| | 0 | 69 | | } |
| | 0 | 70 | | }; |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | public override JsonTypeInfo.ParameterizedConstructorDelegate<T, TArg0, TArg1, TArg2, TArg3>? |
| | | 74 | | CreateParameterizedConstructor<T, TArg0, TArg1, TArg2, TArg3>(ConstructorInfo constructor) |
| | 0 | 75 | | { |
| | 0 | 76 | | Type type = typeof(T); |
| | | 77 | | |
| | 0 | 78 | | Debug.Assert(!type.IsAbstract); |
| | 0 | 79 | | Debug.Assert(constructor.DeclaringType == type && constructor.IsPublic && !constructor.IsStatic); |
| | | 80 | | |
| | 0 | 81 | | int parameterCount = constructor.GetParameters().Length; |
| | | 82 | | |
| | 0 | 83 | | Debug.Assert(parameterCount <= JsonConstants.UnboxedParameterCountThreshold); |
| | | 84 | | |
| | 0 | 85 | | return (arg0, arg1, arg2, arg3) => |
| | 0 | 86 | | { |
| | 0 | 87 | | object[] arguments = new object[parameterCount]; |
| | 0 | 88 | | |
| | 0 | 89 | | for (int i = 0; i < parameterCount; i++) |
| | 0 | 90 | | { |
| | 0 | 91 | | switch (i) |
| | 0 | 92 | | { |
| | 0 | 93 | | case 0: |
| | 0 | 94 | | arguments[0] = arg0!; |
| | 0 | 95 | | break; |
| | 0 | 96 | | case 1: |
| | 0 | 97 | | arguments[1] = arg1!; |
| | 0 | 98 | | break; |
| | 0 | 99 | | case 2: |
| | 0 | 100 | | arguments[2] = arg2!; |
| | 0 | 101 | | break; |
| | 0 | 102 | | case 3: |
| | 0 | 103 | | arguments[3] = arg3!; |
| | 0 | 104 | | break; |
| | 0 | 105 | | default: |
| | 0 | 106 | | Debug.Fail("We shouldn't be here if there are more than 4 parameters."); |
| | 0 | 107 | | throw new InvalidOperationException(); |
| | 0 | 108 | | } |
| | 0 | 109 | | } |
| | 0 | 110 | | |
| | 0 | 111 | | return (T)constructor.Invoke(arguments); |
| | 0 | 112 | | }; |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | public override Action<TCollection, object?> CreateAddMethodDelegate<[DynamicallyAccessedMembers(DynamicallyAcce |
| | 0 | 116 | | { |
| | 0 | 117 | | Type collectionType = typeof(TCollection); |
| | 0 | 118 | | Type elementType = JsonTypeInfo.ObjectType; |
| | | 119 | | |
| | | 120 | | // We verified this won't be null when we created the converter for the collection type. |
| | 0 | 121 | | MethodInfo addMethod = (collectionType.GetMethod("Push") ?? collectionType.GetMethod("Enqueue"))!; |
| | | 122 | | |
| | 0 | 123 | | return delegate (TCollection collection, object? element) |
| | 0 | 124 | | { |
| | 0 | 125 | | addMethod.Invoke(collection, new object[] { element! }); |
| | 0 | 126 | | }; |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | public override Func<IEnumerable<TElement>, TCollection> CreateImmutableEnumerableCreateRangeDelegate<TCollectio |
| | 0 | 130 | | { |
| | 0 | 131 | | MethodInfo createRange = typeof(TCollection).GetImmutableEnumerableCreateRangeMethod(typeof(TElement)); |
| | 0 | 132 | | return (Func<IEnumerable<TElement>, TCollection>)createRange.CreateDelegate( |
| | 0 | 133 | | typeof(Func<IEnumerable<TElement>, TCollection>)); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | public override Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection> CreateImmutableDictionaryCreateRangeD |
| | 0 | 137 | | { |
| | 0 | 138 | | MethodInfo createRange = typeof(TCollection).GetImmutableDictionaryCreateRangeMethod(typeof(TKey), typeof(TV |
| | 0 | 139 | | return (Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection>)createRange.CreateDelegate( |
| | 0 | 140 | | typeof(Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection>)); |
| | 0 | 141 | | } |
| | | 142 | | |
| | | 143 | | public override Func<object, TProperty> CreatePropertyGetter<TProperty>(PropertyInfo propertyInfo) |
| | 0 | 144 | | { |
| | 0 | 145 | | MethodInfo getMethodInfo = propertyInfo.GetMethod!; |
| | | 146 | | |
| | 0 | 147 | | return delegate (object obj) |
| | 0 | 148 | | { |
| | 0 | 149 | | return (TProperty)getMethodInfo.Invoke(obj, null)!; |
| | 0 | 150 | | }; |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | public override Action<object, TProperty> CreatePropertySetter<TProperty>(PropertyInfo propertyInfo) |
| | 0 | 154 | | { |
| | 0 | 155 | | MethodInfo setMethodInfo = propertyInfo.SetMethod!; |
| | | 156 | | |
| | 0 | 157 | | return delegate (object obj, TProperty value) |
| | 0 | 158 | | { |
| | 0 | 159 | | setMethodInfo.Invoke(obj, new object[] { value! }); |
| | 0 | 160 | | }; |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | public override Func<object, TProperty> CreateFieldGetter<TProperty>(FieldInfo fieldInfo) => |
| | 0 | 164 | | delegate (object obj) |
| | 0 | 165 | | { |
| | 0 | 166 | | return (TProperty)fieldInfo.GetValue(obj)!; |
| | 0 | 167 | | }; |
| | | 168 | | |
| | | 169 | | public override Action<object, TProperty> CreateFieldSetter<TProperty>(FieldInfo fieldInfo) => |
| | 0 | 170 | | delegate (object obj, TProperty value) |
| | 0 | 171 | | { |
| | 0 | 172 | | fieldInfo.SetValue(obj, value); |
| | 0 | 173 | | }; |
| | | 174 | | } |
| | | 175 | | } |