| | | 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 | | using System.Text.Json.Reflection; |
| | | 9 | | |
| | | 10 | | namespace System.Text.Json.Serialization.Metadata |
| | | 11 | | { |
| | | 12 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 13 | | [RequiresUnreferencedCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 14 | | internal sealed class ReflectionMemberAccessor : MemberAccessor |
| | | 15 | | { |
| | 0 | 16 | | public ReflectionMemberAccessor() |
| | 0 | 17 | | { |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | public override Func<object>? CreateParameterlessConstructor(Type type, ConstructorInfo? ctorInfo) |
| | 0 | 21 | | { |
| | 0 | 22 | | Debug.Assert(type != null); |
| | 0 | 23 | | Debug.Assert(ctorInfo is null || ctorInfo.GetParameters().Length == 0); |
| | | 24 | | |
| | 0 | 25 | | if (type.IsAbstract) |
| | 0 | 26 | | { |
| | 0 | 27 | | return null; |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | if (ctorInfo is null) |
| | 0 | 31 | | { |
| | 0 | 32 | | return type.IsValueType |
| | 0 | 33 | | ? () => Activator.CreateInstance(type, nonPublic: false)! |
| | 0 | 34 | | : null; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | #if NET |
| | 0 | 38 | | ConstructorInvoker invoker = ConstructorInvoker.Create(ctorInfo); |
| | 0 | 39 | | return invoker.Invoke; |
| | | 40 | | #else |
| | | 41 | | return () => ctorInfo.InvokeNoWrapExceptions(null); |
| | | 42 | | #endif |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | public override Func<object?[], T> CreateParameterizedConstructor<T>(ConstructorInfo constructor) |
| | 0 | 46 | | { |
| | 0 | 47 | | Type type = typeof(T); |
| | | 48 | | |
| | 0 | 49 | | Debug.Assert(!type.IsAbstract); |
| | 0 | 50 | | Debug.Assert(constructor.DeclaringType == type && !constructor.IsStatic); |
| | | 51 | | |
| | 0 | 52 | | int parameterCount = constructor.GetParameters().Length; |
| | | 53 | | |
| | | 54 | | #if NET |
| | 0 | 55 | | ConstructorInvoker invoker = ConstructorInvoker.Create(constructor); |
| | 0 | 56 | | return arguments => (T)invoker.Invoke(arguments.AsSpan(0, parameterCount)); |
| | | 57 | | #else |
| | | 58 | | return (arguments) => |
| | | 59 | | { |
| | | 60 | | // The input array was rented from the shared ArrayPool, so its size is likely to be larger than the par |
| | | 61 | | // The emit equivalent of this method does not (need to) allocate here + transfer the objects. |
| | | 62 | | object[] argsToPass = new object[parameterCount]; |
| | | 63 | | |
| | | 64 | | Array.Copy(arguments, 0, argsToPass, 0, parameterCount); |
| | | 65 | | |
| | | 66 | | // Not wrapping in TargetInvocationException also plumbs ArgumentException through for |
| | | 67 | | // tuples with more than 7 generic parameters, e.g. |
| | | 68 | | // System.ArgumentException : The last element of an eight element tuple must be a Tuple. |
| | | 69 | | return (T)constructor.InvokeNoWrapExceptions(argsToPass); |
| | | 70 | | }; |
| | | 71 | | #endif |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | public override JsonTypeInfo.ParameterizedConstructorDelegate<T, TArg0, TArg1, TArg2, TArg3>? |
| | | 75 | | CreateParameterizedConstructor<T, TArg0, TArg1, TArg2, TArg3>(ConstructorInfo constructor) |
| | 0 | 76 | | { |
| | 0 | 77 | | Type type = typeof(T); |
| | | 78 | | |
| | 0 | 79 | | Debug.Assert(!type.IsAbstract); |
| | 0 | 80 | | Debug.Assert(constructor.DeclaringType == type && !constructor.IsStatic); |
| | | 81 | | |
| | 0 | 82 | | int parameterCount = constructor.GetParameters().Length; |
| | | 83 | | #if NET |
| | 0 | 84 | | ConstructorInvoker invoker = ConstructorInvoker.Create(constructor); |
| | | 85 | | #endif |
| | | 86 | | |
| | 0 | 87 | | Debug.Assert(parameterCount <= JsonConstants.UnboxedParameterCountThreshold); |
| | | 88 | | |
| | 0 | 89 | | return (arg0, arg1, arg2, arg3) => |
| | 0 | 90 | | { |
| | 0 | 91 | | #if NET |
| | 0 | 92 | | switch (parameterCount) |
| | 0 | 93 | | { |
| | 0 | 94 | | case 0: |
| | 0 | 95 | | return (T)invoker.Invoke(); |
| | 0 | 96 | | case 1: |
| | 0 | 97 | | return (T)invoker.Invoke(arg0); |
| | 0 | 98 | | case 2: |
| | 0 | 99 | | return (T)invoker.Invoke(arg0, arg1); |
| | 0 | 100 | | case 3: |
| | 0 | 101 | | return (T)invoker.Invoke(arg0, arg1, arg2); |
| | 0 | 102 | | case 4: |
| | 0 | 103 | | return (T)invoker.Invoke(arg0, arg1, arg2, arg3); |
| | 0 | 104 | | default: |
| | 0 | 105 | | Debug.Fail("We shouldn't be here if there are more than 4 parameters."); |
| | 0 | 106 | | throw new InvalidOperationException(); |
| | 0 | 107 | | } |
| | 0 | 108 | | #else |
| | 0 | 109 | | object?[] arguments = new object?[parameterCount]; |
| | 0 | 110 | | |
| | 0 | 111 | | switch (parameterCount) |
| | 0 | 112 | | { |
| | 0 | 113 | | case > 4: |
| | 0 | 114 | | Debug.Fail("We shouldn't be here if there are more than 4 parameters."); |
| | 0 | 115 | | throw new InvalidOperationException(); |
| | 0 | 116 | | case 4: |
| | 0 | 117 | | arguments[3] = arg3; |
| | 0 | 118 | | goto case 3; |
| | 0 | 119 | | case 3: |
| | 0 | 120 | | arguments[2] = arg2; |
| | 0 | 121 | | goto case 2; |
| | 0 | 122 | | case 2: |
| | 0 | 123 | | arguments[1] = arg1; |
| | 0 | 124 | | goto case 1; |
| | 0 | 125 | | case 1: |
| | 0 | 126 | | arguments[0] = arg0; |
| | 0 | 127 | | break; |
| | 0 | 128 | | } |
| | 0 | 129 | | |
| | 0 | 130 | | return (T)constructor.InvokeNoWrapExceptions(arguments); |
| | 0 | 131 | | #endif |
| | 0 | 132 | | }; |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | public override Func<object?, T> CreateSingleParameterConstructor<T>(ConstructorInfo constructor) |
| | 0 | 136 | | { |
| | 0 | 137 | | Type type = typeof(T); |
| | | 138 | | |
| | 0 | 139 | | Debug.Assert(!type.IsAbstract); |
| | 0 | 140 | | Debug.Assert(constructor.DeclaringType == type && !constructor.IsStatic); |
| | 0 | 141 | | Debug.Assert(constructor.GetParameters().Length == 1); |
| | | 142 | | |
| | | 143 | | #if NET |
| | 0 | 144 | | ConstructorInvoker invoker = ConstructorInvoker.Create(constructor); |
| | 0 | 145 | | return value => (T)invoker.Invoke(value); |
| | | 146 | | #else |
| | | 147 | | return value => (T)constructor.InvokeNoWrapExceptions(new object?[] { value }); |
| | | 148 | | #endif |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | public override Action<TCollection, object?> CreateAddMethodDelegate<[DynamicallyAccessedMembers(DynamicallyAcce |
| | 0 | 152 | | { |
| | 0 | 153 | | Type collectionType = typeof(TCollection); |
| | 0 | 154 | | Type elementType = typeof(object); |
| | | 155 | | |
| | | 156 | | // We verified this won't be null when we created the converter for the collection type. |
| | 0 | 157 | | MethodInfo addMethod = (collectionType.GetMethod("Push") ?? collectionType.GetMethod("Enqueue"))!; |
| | | 158 | | #if NET |
| | 0 | 159 | | MethodInvoker invoker = MethodInvoker.Create(addMethod); |
| | 0 | 160 | | return (collection, element) => invoker.Invoke(collection, element); |
| | | 161 | | #else |
| | | 162 | | return (collection, element) => addMethod.InvokeNoWrapExceptions(collection, new object[] { element }); |
| | | 163 | | #endif |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | public override Func<IEnumerable<TElement>, TCollection> CreateImmutableEnumerableCreateRangeDelegate<TCollectio |
| | 0 | 167 | | { |
| | 0 | 168 | | MethodInfo createRange = typeof(TCollection).GetImmutableEnumerableCreateRangeMethod(typeof(TElement)); |
| | 0 | 169 | | return (Func<IEnumerable<TElement>, TCollection>)createRange.CreateDelegate( |
| | 0 | 170 | | typeof(Func<IEnumerable<TElement>, TCollection>)); |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | public override Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection> CreateImmutableDictionaryCreateRangeD |
| | 0 | 174 | | { |
| | 0 | 175 | | MethodInfo createRange = typeof(TCollection).GetImmutableDictionaryCreateRangeMethod(typeof(TKey), typeof(TV |
| | 0 | 176 | | return (Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection>)createRange.CreateDelegate( |
| | 0 | 177 | | typeof(Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection>)); |
| | 0 | 178 | | } |
| | | 179 | | |
| | | 180 | | public override Func<object, TProperty> CreatePropertyGetter<TProperty>(PropertyInfo propertyInfo) |
| | 0 | 181 | | { |
| | 0 | 182 | | MethodInfo getMethodInfo = propertyInfo.GetMethod!; |
| | | 183 | | #if NET |
| | 0 | 184 | | MethodInvoker invoker = MethodInvoker.Create(getMethodInfo); |
| | 0 | 185 | | return obj => (TProperty)invoker.Invoke(obj)!; |
| | | 186 | | #else |
| | | 187 | | return obj => (TProperty)getMethodInfo.InvokeNoWrapExceptions(obj, null)!; |
| | | 188 | | #endif |
| | 0 | 189 | | } |
| | | 190 | | |
| | | 191 | | private delegate TProperty ValueTypePropertyGetter<TProperty, TDeclaringType>(ref TDeclaringType obj); |
| | | 192 | | |
| | | 193 | | public override Func<TDeclaringType, TProperty> CreatePropertyGetter<TDeclaringType, TProperty>(PropertyInfo pro |
| | 0 | 194 | | { |
| | 0 | 195 | | MethodInfo getMethodInfo = propertyInfo.GetMethod!; |
| | | 196 | | |
| | | 197 | | // If TDeclaringType is a value type, the instance is passed by reference, |
| | | 198 | | // so we need to create a wrapper delegate to account for this. |
| | 0 | 199 | | if (typeof(TDeclaringType).IsValueType) |
| | 0 | 200 | | { |
| | 0 | 201 | | ValueTypePropertyGetter<TProperty, TDeclaringType> f = getMethodInfo.CreateDelegate<ValueTypePropertyGet |
| | 0 | 202 | | return obj => f(ref obj); |
| | | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | return getMethodInfo.CreateDelegate<Func<TDeclaringType, TProperty>>(); |
| | 0 | 206 | | } |
| | | 207 | | |
| | | 208 | | public override Action<object, TProperty> CreatePropertySetter<TProperty>(PropertyInfo propertyInfo) |
| | 0 | 209 | | { |
| | 0 | 210 | | MethodInfo setMethodInfo = propertyInfo.SetMethod!; |
| | | 211 | | |
| | | 212 | | #if NET |
| | 0 | 213 | | MethodInvoker invoker = MethodInvoker.Create(setMethodInfo); |
| | 0 | 214 | | return (obj, value) => invoker.Invoke(obj, value); |
| | | 215 | | #else |
| | | 216 | | return (obj, value) => setMethodInfo.InvokeNoWrapExceptions(obj, new object[] { value }); |
| | | 217 | | #endif |
| | 0 | 218 | | } |
| | | 219 | | |
| | | 220 | | public override Func<object, TProperty> CreateFieldGetter<TProperty>(FieldInfo fieldInfo) => |
| | 0 | 221 | | delegate (object obj) |
| | 0 | 222 | | { |
| | 0 | 223 | | return (TProperty)fieldInfo.GetValue(obj)!; |
| | 0 | 224 | | }; |
| | | 225 | | |
| | | 226 | | public override Action<object, TProperty> CreateFieldSetter<TProperty>(FieldInfo fieldInfo) => |
| | 0 | 227 | | delegate (object obj, TProperty value) |
| | 0 | 228 | | { |
| | 0 | 229 | | fieldInfo.SetValue(obj, value); |
| | 0 | 230 | | }; |
| | | 231 | | |
| | | 232 | | public override UnionTryGetValueAccessor<TUnion> CreateUnionTryGetValueAccessor<TUnion>(IReadOnlyList<KeyValuePa |
| | 0 | 233 | | { |
| | | 234 | | // Build per-entry typed delegates via Delegate.CreateDelegate so each TryGetValue |
| | | 235 | | // call is a direct invocation rather than MethodInfo.Invoke (which would box |
| | | 236 | | // value-type unions and allocate per call). Then return a closure that walks the |
| | | 237 | | // chain in caller-supplied order; first match wins. |
| | 0 | 238 | | int count = entries.Count; |
| | 0 | 239 | | Type[] caseTypes = new Type[count]; |
| | 0 | 240 | | UnionTryGetValueAccessor<TUnion>[] chain = new UnionTryGetValueAccessor<TUnion>[count]; |
| | 0 | 241 | | for (int i = 0; i < count; i++) |
| | 0 | 242 | | { |
| | 0 | 243 | | KeyValuePair<Type, MethodInfo> entry = entries[i]; |
| | 0 | 244 | | caseTypes[i] = entry.Key; |
| | 0 | 245 | | chain[i] = (UnionTryGetValueAccessor<TUnion>)typeof(ReflectionMemberAccessor) |
| | 0 | 246 | | .GetMethod(nameof(CreateUnionTryGetValueAccessorCore), BindingFlags.NonPublic | BindingFlags.Static) |
| | 0 | 247 | | .MakeGenericMethod(typeof(TUnion), entry.Key) |
| | 0 | 248 | | .Invoke(null, new object[] { entry.Value })!; |
| | 0 | 249 | | } |
| | | 250 | | |
| | 0 | 251 | | return (TUnion union, out Type? caseType, out object? value) => |
| | 0 | 252 | | { |
| | 0 | 253 | | for (int i = 0; i < chain.Length; i++) |
| | 0 | 254 | | { |
| | 0 | 255 | | if (chain[i](union, out _, out value)) |
| | 0 | 256 | | { |
| | 0 | 257 | | caseType = caseTypes[i]; |
| | 0 | 258 | | return true; |
| | 0 | 259 | | } |
| | 0 | 260 | | } |
| | 0 | 261 | | |
| | 0 | 262 | | caseType = null; |
| | 0 | 263 | | value = null; |
| | 0 | 264 | | return false; |
| | 0 | 265 | | }; |
| | 0 | 266 | | } |
| | | 267 | | |
| | | 268 | | private delegate bool TypedTryGetValueDelegate<TUnion, TCase>(TUnion union, out TCase? value); |
| | | 269 | | |
| | | 270 | | private delegate bool TypedStructTryGetValueDelegate<TUnion, TCase>(ref TUnion union, out TCase? value); |
| | | 271 | | |
| | | 272 | | private static UnionTryGetValueAccessor<TUnion> CreateUnionTryGetValueAccessorCore<TUnion, TCase>(MethodInfo met |
| | 0 | 273 | | { |
| | | 274 | | // Per-entry adapter: binds the user-declared method to a typed delegate and |
| | | 275 | | // returns a delegate matching UnionTryGetValueAccessor<TUnion>. The outer chained |
| | | 276 | | // delegate fills in caseType on success; this inner adapter only reports value. |
| | 0 | 277 | | if (typeof(TUnion).IsValueType) |
| | 0 | 278 | | { |
| | 0 | 279 | | TypedStructTryGetValueDelegate<TUnion, TCase> typed = |
| | 0 | 280 | | (TypedStructTryGetValueDelegate<TUnion, TCase>)Delegate.CreateDelegate( |
| | 0 | 281 | | typeof(TypedStructTryGetValueDelegate<TUnion, TCase>), method, throwOnBindFailure: true)!; |
| | | 282 | | |
| | 0 | 283 | | return (TUnion union, out Type? caseType, out object? value) => |
| | 0 | 284 | | { |
| | 0 | 285 | | bool result = typed(ref union, out TCase? extracted); |
| | 0 | 286 | | caseType = null; |
| | 0 | 287 | | value = result ? extracted : null; |
| | 0 | 288 | | return result; |
| | 0 | 289 | | }; |
| | | 290 | | } |
| | | 291 | | else |
| | 0 | 292 | | { |
| | 0 | 293 | | TypedTryGetValueDelegate<TUnion, TCase> typed = |
| | 0 | 294 | | (TypedTryGetValueDelegate<TUnion, TCase>)Delegate.CreateDelegate( |
| | 0 | 295 | | typeof(TypedTryGetValueDelegate<TUnion, TCase>), method, throwOnBindFailure: true)!; |
| | | 296 | | |
| | 0 | 297 | | return (TUnion union, out Type? caseType, out object? value) => |
| | 0 | 298 | | { |
| | 0 | 299 | | bool result = typed(union, out TCase? extracted); |
| | 0 | 300 | | caseType = null; |
| | 0 | 301 | | value = result ? extracted : null; |
| | 0 | 302 | | return result; |
| | 0 | 303 | | }; |
| | | 304 | | } |
| | 0 | 305 | | } |
| | | 306 | | } |
| | | 307 | | } |
| | | 308 | | |