| | | 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; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Reflection; |
| | | 9 | | using System.Reflection.Emit; |
| | | 10 | | |
| | | 11 | | namespace System.Text.Json.Serialization.Metadata |
| | | 12 | | { |
| | | 13 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 14 | | [RequiresUnreferencedCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | | 15 | | internal sealed class ReflectionEmitMemberAccessor : MemberAccessor |
| | | 16 | | { |
| | 1 | 17 | | public ReflectionEmitMemberAccessor() |
| | 1 | 18 | | { |
| | 1 | 19 | | } |
| | | 20 | | |
| | | 21 | | public override Func<object>? CreateParameterlessConstructor(Type type, ConstructorInfo? constructorInfo) |
| | 1071 | 22 | | { |
| | 1071 | 23 | | Debug.Assert(type != null); |
| | 1071 | 24 | | Debug.Assert(constructorInfo is null || constructorInfo.GetParameters().Length == 0); |
| | | 25 | | |
| | 1071 | 26 | | if (type.IsAbstract) |
| | 20 | 27 | | { |
| | 20 | 28 | | return null; |
| | | 29 | | } |
| | | 30 | | |
| | 1051 | 31 | | if (constructorInfo is null && !type.IsValueType) |
| | 387 | 32 | | { |
| | 387 | 33 | | return null; |
| | | 34 | | } |
| | | 35 | | |
| | 664 | 36 | | var dynamicMethod = new DynamicMethod( |
| | 664 | 37 | | ConstructorInfo.ConstructorName, |
| | 664 | 38 | | typeof(object), |
| | 664 | 39 | | Type.EmptyTypes, |
| | 664 | 40 | | typeof(ReflectionEmitMemberAccessor).Module, |
| | 664 | 41 | | skipVisibility: true); |
| | | 42 | | |
| | 664 | 43 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | | 44 | | |
| | 664 | 45 | | if (constructorInfo is null) |
| | 270 | 46 | | { |
| | 270 | 47 | | Debug.Assert(type.IsValueType); |
| | | 48 | | |
| | 270 | 49 | | LocalBuilder local = generator.DeclareLocal(type); |
| | | 50 | | |
| | 270 | 51 | | generator.Emit(OpCodes.Ldloca_S, local); |
| | 270 | 52 | | generator.Emit(OpCodes.Initobj, type); |
| | 270 | 53 | | generator.Emit(OpCodes.Ldloc, local); |
| | 270 | 54 | | generator.Emit(OpCodes.Box, type); |
| | 270 | 55 | | } |
| | | 56 | | else |
| | 394 | 57 | | { |
| | 394 | 58 | | generator.Emit(OpCodes.Newobj, constructorInfo); |
| | 394 | 59 | | if (type.IsValueType) |
| | 0 | 60 | | { |
| | | 61 | | // Since C# 10 it's now possible to have parameterless constructors in structs |
| | 0 | 62 | | generator.Emit(OpCodes.Box, type); |
| | 0 | 63 | | } |
| | 394 | 64 | | } |
| | | 65 | | |
| | 664 | 66 | | generator.Emit(OpCodes.Ret); |
| | | 67 | | |
| | 664 | 68 | | return CreateDelegate<Func<object>>(dynamicMethod); |
| | 1071 | 69 | | } |
| | | 70 | | |
| | | 71 | | public override Func<object?[], T> CreateParameterizedConstructor<T>(ConstructorInfo constructor) => |
| | 326 | 72 | | CreateDelegate<Func<object?[], T>>(CreateParameterizedConstructor(constructor)); |
| | | 73 | | |
| | | 74 | | private static DynamicMethod CreateParameterizedConstructor(ConstructorInfo constructor) |
| | 326 | 75 | | { |
| | 326 | 76 | | Type? type = constructor.DeclaringType; |
| | | 77 | | |
| | 326 | 78 | | Debug.Assert(type != null); |
| | 326 | 79 | | Debug.Assert(!type.IsAbstract); |
| | 326 | 80 | | Debug.Assert(!constructor.IsStatic); |
| | | 81 | | |
| | 326 | 82 | | ParameterInfo[] parameters = constructor.GetParameters(); |
| | 326 | 83 | | int parameterCount = parameters.Length; |
| | | 84 | | |
| | 326 | 85 | | var dynamicMethod = new DynamicMethod( |
| | 326 | 86 | | ConstructorInfo.ConstructorName, |
| | 326 | 87 | | type, |
| | 326 | 88 | | new[] { typeof(object[]) }, |
| | 326 | 89 | | typeof(ReflectionEmitMemberAccessor).Module, |
| | 326 | 90 | | skipVisibility: true); |
| | | 91 | | |
| | 326 | 92 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | | 93 | | |
| | | 94 | | // For byref parameters, we need to store values in local variables and pass addresses. |
| | | 95 | | // For out parameters, we just need a default-initialized local to pass by address. |
| | 326 | 96 | | LocalBuilder?[] locals = new LocalBuilder?[parameterCount]; |
| | | 97 | | |
| | | 98 | | // Track the mapping from constructor parameter index to args[] index. |
| | | 99 | | // out parameters don't have entries in args[]. |
| | 326 | 100 | | int argsIndex = 0; |
| | 326 | 101 | | int[] argsIndices = new int[parameterCount]; |
| | 4564 | 102 | | for (int i = 0; i < parameterCount; i++) |
| | 1956 | 103 | | { |
| | 1956 | 104 | | if (parameters[i].IsOut) |
| | 0 | 105 | | { |
| | 0 | 106 | | argsIndices[i] = -1; // out parameters don't have an args entry |
| | 0 | 107 | | } |
| | | 108 | | else |
| | 1956 | 109 | | { |
| | 1956 | 110 | | argsIndices[i] = argsIndex++; |
| | 1956 | 111 | | } |
| | 1956 | 112 | | } |
| | | 113 | | |
| | 4564 | 114 | | for (int i = 0; i < parameterCount; i++) |
| | 1956 | 115 | | { |
| | 1956 | 116 | | Type paramType = parameters[i].ParameterType; |
| | 1956 | 117 | | if (paramType.IsByRef) |
| | 0 | 118 | | { |
| | | 119 | | // Declare a local for the underlying type. |
| | 0 | 120 | | Type elementType = paramType.GetElementType()!; |
| | 0 | 121 | | locals[i] = generator.DeclareLocal(elementType); |
| | | 122 | | |
| | 0 | 123 | | if (parameters[i].IsOut) |
| | 0 | 124 | | { |
| | | 125 | | // For out parameters, just initialize the local to default. |
| | | 126 | | // We don't load from args[] since out params aren't in the metadata. |
| | 0 | 127 | | if (elementType.IsValueType) |
| | 0 | 128 | | { |
| | 0 | 129 | | generator.Emit(OpCodes.Ldloca, locals[i]!); |
| | 0 | 130 | | generator.Emit(OpCodes.Initobj, elementType); |
| | 0 | 131 | | } |
| | | 132 | | else |
| | 0 | 133 | | { |
| | 0 | 134 | | generator.Emit(OpCodes.Ldnull); |
| | 0 | 135 | | generator.Emit(OpCodes.Stloc, locals[i]!); |
| | 0 | 136 | | } |
| | 0 | 137 | | } |
| | | 138 | | else |
| | 0 | 139 | | { |
| | | 140 | | // Load value from object array, unbox it, and store in the local. |
| | 0 | 141 | | generator.Emit(OpCodes.Ldarg_0); |
| | 0 | 142 | | generator.Emit(OpCodes.Ldc_I4, argsIndices[i]); |
| | 0 | 143 | | generator.Emit(OpCodes.Ldelem_Ref); |
| | 0 | 144 | | generator.Emit(OpCodes.Unbox_Any, elementType); |
| | 0 | 145 | | generator.Emit(OpCodes.Stloc, locals[i]!); |
| | 0 | 146 | | } |
| | 0 | 147 | | } |
| | 1956 | 148 | | } |
| | | 149 | | |
| | | 150 | | // Now push all arguments onto the stack. |
| | 4564 | 151 | | for (int i = 0; i < parameterCount; i++) |
| | 1956 | 152 | | { |
| | 1956 | 153 | | Type paramType = parameters[i].ParameterType; |
| | | 154 | | |
| | 1956 | 155 | | if (paramType.IsByRef) |
| | 0 | 156 | | { |
| | | 157 | | // Load address of the local variable. |
| | 0 | 158 | | generator.Emit(OpCodes.Ldloca, locals[i]!); |
| | 0 | 159 | | } |
| | | 160 | | else |
| | 1956 | 161 | | { |
| | | 162 | | // Load value from object array and unbox. |
| | 1956 | 163 | | generator.Emit(OpCodes.Ldarg_0); |
| | 1956 | 164 | | generator.Emit(OpCodes.Ldc_I4, argsIndices[i]); |
| | 1956 | 165 | | generator.Emit(OpCodes.Ldelem_Ref); |
| | 1956 | 166 | | generator.Emit(OpCodes.Unbox_Any, paramType); |
| | 1956 | 167 | | } |
| | 1956 | 168 | | } |
| | | 169 | | |
| | 326 | 170 | | generator.Emit(OpCodes.Newobj, constructor); |
| | 326 | 171 | | generator.Emit(OpCodes.Ret); |
| | | 172 | | |
| | 326 | 173 | | return dynamicMethod; |
| | 326 | 174 | | } |
| | | 175 | | |
| | | 176 | | public override JsonTypeInfo.ParameterizedConstructorDelegate<T, TArg0, TArg1, TArg2, TArg3>? |
| | | 177 | | CreateParameterizedConstructor<T, TArg0, TArg1, TArg2, TArg3>(ConstructorInfo constructor) => |
| | 0 | 178 | | CreateDelegate<JsonTypeInfo.ParameterizedConstructorDelegate<T, TArg0, TArg1, TArg2, TArg3>>( |
| | 0 | 179 | | CreateParameterizedConstructor(constructor, typeof(TArg0), typeof(TArg1), typeof(TArg2), typeof(TArg3))) |
| | | 180 | | |
| | | 181 | | public override Func<object?, T> CreateSingleParameterConstructor<T>(ConstructorInfo constructor) => |
| | 0 | 182 | | CreateDelegate<Func<object?, T>>(CreateSingleParameterConstructor(constructor)); |
| | | 183 | | |
| | | 184 | | private static DynamicMethod CreateSingleParameterConstructor(ConstructorInfo constructor) |
| | 0 | 185 | | { |
| | 0 | 186 | | Type? type = constructor.DeclaringType; |
| | | 187 | | |
| | 0 | 188 | | Debug.Assert(type != null); |
| | 0 | 189 | | Debug.Assert(!type.IsAbstract); |
| | 0 | 190 | | Debug.Assert(!constructor.IsStatic); |
| | | 191 | | |
| | 0 | 192 | | ParameterInfo[] parameters = constructor.GetParameters(); |
| | 0 | 193 | | Debug.Assert(parameters.Length == 1); |
| | | 194 | | |
| | 0 | 195 | | Type parameterType = parameters[0].ParameterType; |
| | 0 | 196 | | Debug.Assert(!parameterType.IsByRef); |
| | | 197 | | |
| | 0 | 198 | | var dynamicMethod = new DynamicMethod( |
| | 0 | 199 | | ConstructorInfo.ConstructorName, |
| | 0 | 200 | | type, |
| | 0 | 201 | | new[] { typeof(object) }, |
| | 0 | 202 | | typeof(ReflectionEmitMemberAccessor).Module, |
| | 0 | 203 | | skipVisibility: true); |
| | | 204 | | |
| | 0 | 205 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | 0 | 206 | | EmitObjectArgument(generator, parameterType); |
| | 0 | 207 | | generator.Emit(OpCodes.Newobj, constructor); |
| | 0 | 208 | | generator.Emit(OpCodes.Ret); |
| | | 209 | | |
| | 0 | 210 | | return dynamicMethod; |
| | 0 | 211 | | } |
| | | 212 | | |
| | | 213 | | private static void EmitObjectArgument(ILGenerator generator, Type parameterType) |
| | 0 | 214 | | { |
| | 0 | 215 | | Type? nullableUnderlyingType = Nullable.GetUnderlyingType(parameterType); |
| | 0 | 216 | | if (nullableUnderlyingType is not null) |
| | 0 | 217 | | { |
| | 0 | 218 | | Label notNull = generator.DefineLabel(); |
| | 0 | 219 | | Label done = generator.DefineLabel(); |
| | 0 | 220 | | LocalBuilder nullableLocal = generator.DeclareLocal(parameterType); |
| | | 221 | | |
| | 0 | 222 | | generator.Emit(OpCodes.Ldarg_0); |
| | 0 | 223 | | generator.Emit(OpCodes.Brtrue_S, notNull); |
| | | 224 | | |
| | 0 | 225 | | generator.Emit(OpCodes.Ldloca_S, nullableLocal); |
| | 0 | 226 | | generator.Emit(OpCodes.Initobj, parameterType); |
| | 0 | 227 | | generator.Emit(OpCodes.Ldloc, nullableLocal); |
| | 0 | 228 | | generator.Emit(OpCodes.Br_S, done); |
| | | 229 | | |
| | 0 | 230 | | generator.MarkLabel(notNull); |
| | 0 | 231 | | generator.Emit(OpCodes.Ldarg_0); |
| | 0 | 232 | | generator.Emit(OpCodes.Unbox_Any, nullableUnderlyingType); |
| | 0 | 233 | | generator.Emit(OpCodes.Newobj, parameterType.GetConstructor(new[] { nullableUnderlyingType })!); |
| | | 234 | | |
| | 0 | 235 | | generator.MarkLabel(done); |
| | 0 | 236 | | } |
| | | 237 | | else |
| | 0 | 238 | | { |
| | 0 | 239 | | generator.Emit(OpCodes.Ldarg_0); |
| | 0 | 240 | | generator.Emit(parameterType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, parameterType); |
| | 0 | 241 | | } |
| | 0 | 242 | | } |
| | | 243 | | |
| | | 244 | | private static DynamicMethod? CreateParameterizedConstructor(ConstructorInfo constructor, Type parameterType1, T |
| | 0 | 245 | | { |
| | 0 | 246 | | Type? type = constructor.DeclaringType; |
| | | 247 | | |
| | 0 | 248 | | Debug.Assert(type != null); |
| | 0 | 249 | | Debug.Assert(!type.IsAbstract); |
| | 0 | 250 | | Debug.Assert(!constructor.IsStatic); |
| | | 251 | | |
| | 0 | 252 | | ParameterInfo[] parameters = constructor.GetParameters(); |
| | 0 | 253 | | int parameterCount = parameters.Length; |
| | | 254 | | |
| | 0 | 255 | | var dynamicMethod = new DynamicMethod( |
| | 0 | 256 | | ConstructorInfo.ConstructorName, |
| | 0 | 257 | | type, |
| | 0 | 258 | | new[] { parameterType1, parameterType2, parameterType3, parameterType4 }, |
| | 0 | 259 | | typeof(ReflectionEmitMemberAccessor).Module, |
| | 0 | 260 | | skipVisibility: true); |
| | | 261 | | |
| | 0 | 262 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | | 263 | | |
| | | 264 | | // For byref parameters, we need locals because we receive values but need to pass addresses. |
| | 0 | 265 | | LocalBuilder?[] locals = new LocalBuilder?[parameterCount]; |
| | 0 | 266 | | for (int index = 0; index < parameterCount; index++) |
| | 0 | 267 | | { |
| | 0 | 268 | | if (parameters[index].ParameterType.IsByRef) |
| | 0 | 269 | | { |
| | 0 | 270 | | Type elementType = parameters[index].ParameterType.GetElementType()!; |
| | 0 | 271 | | locals[index] = generator.DeclareLocal(elementType); |
| | | 272 | | |
| | 0 | 273 | | if (parameters[index].IsOut) |
| | 0 | 274 | | { |
| | | 275 | | // For out parameters, initialize to default (the caller passes placeholder values). |
| | 0 | 276 | | if (elementType.IsValueType) |
| | 0 | 277 | | { |
| | 0 | 278 | | generator.Emit(OpCodes.Ldloca, locals[index]!); |
| | 0 | 279 | | generator.Emit(OpCodes.Initobj, elementType); |
| | 0 | 280 | | } |
| | | 281 | | else |
| | 0 | 282 | | { |
| | 0 | 283 | | generator.Emit(OpCodes.Ldnull); |
| | 0 | 284 | | generator.Emit(OpCodes.Stloc, locals[index]!); |
| | 0 | 285 | | } |
| | 0 | 286 | | } |
| | | 287 | | else |
| | 0 | 288 | | { |
| | | 289 | | // For in/ref parameters, copy the argument value to the local. |
| | 0 | 290 | | generator.Emit( |
| | 0 | 291 | | index switch |
| | 0 | 292 | | { |
| | 0 | 293 | | 0 => OpCodes.Ldarg_0, |
| | 0 | 294 | | 1 => OpCodes.Ldarg_1, |
| | 0 | 295 | | 2 => OpCodes.Ldarg_2, |
| | 0 | 296 | | 3 => OpCodes.Ldarg_3, |
| | 0 | 297 | | _ => throw new InvalidOperationException() |
| | 0 | 298 | | }); |
| | 0 | 299 | | generator.Emit(OpCodes.Stloc, locals[index]!); |
| | 0 | 300 | | } |
| | 0 | 301 | | } |
| | 0 | 302 | | } |
| | | 303 | | |
| | | 304 | | // Now push all arguments onto the stack. |
| | 0 | 305 | | for (int index = 0; index < parameterCount; index++) |
| | 0 | 306 | | { |
| | 0 | 307 | | Debug.Assert(index <= JsonConstants.UnboxedParameterCountThreshold); |
| | | 308 | | |
| | 0 | 309 | | if (parameters[index].ParameterType.IsByRef) |
| | 0 | 310 | | { |
| | | 311 | | // Load address of the local variable. |
| | 0 | 312 | | generator.Emit(OpCodes.Ldloca, locals[index]!); |
| | 0 | 313 | | } |
| | | 314 | | else |
| | 0 | 315 | | { |
| | | 316 | | // Load the argument value directly. |
| | 0 | 317 | | generator.Emit( |
| | 0 | 318 | | index switch |
| | 0 | 319 | | { |
| | 0 | 320 | | 0 => OpCodes.Ldarg_0, |
| | 0 | 321 | | 1 => OpCodes.Ldarg_1, |
| | 0 | 322 | | 2 => OpCodes.Ldarg_2, |
| | 0 | 323 | | 3 => OpCodes.Ldarg_3, |
| | 0 | 324 | | _ => throw new InvalidOperationException() |
| | 0 | 325 | | }); |
| | 0 | 326 | | } |
| | 0 | 327 | | } |
| | | 328 | | |
| | 0 | 329 | | generator.Emit(OpCodes.Newobj, constructor); |
| | 0 | 330 | | generator.Emit(OpCodes.Ret); |
| | | 331 | | |
| | 0 | 332 | | return dynamicMethod; |
| | 0 | 333 | | } |
| | | 334 | | |
| | | 335 | | public override Action<TCollection, object?> CreateAddMethodDelegate<[DynamicallyAccessedMembers(DynamicallyAcce |
| | 0 | 336 | | CreateDelegate<Action<TCollection, object?>>(CreateAddMethodDelegate(typeof(TCollection))); |
| | | 337 | | |
| | | 338 | | private static DynamicMethod CreateAddMethodDelegate( |
| | | 339 | | [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type collectionType) |
| | 0 | 340 | | { |
| | | 341 | | // We verified this won't be null when we created the converter that calls this method. |
| | 0 | 342 | | MethodInfo realMethod = (collectionType.GetMethod("Push") ?? collectionType.GetMethod("Enqueue"))!; |
| | | 343 | | |
| | 0 | 344 | | var dynamicMethod = new DynamicMethod( |
| | 0 | 345 | | realMethod.Name, |
| | 0 | 346 | | typeof(void), |
| | 0 | 347 | | new[] { collectionType, typeof(object) }, |
| | 0 | 348 | | typeof(ReflectionEmitMemberAccessor).Module, |
| | 0 | 349 | | skipVisibility: true); |
| | | 350 | | |
| | 0 | 351 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | | 352 | | |
| | 0 | 353 | | generator.Emit(OpCodes.Ldarg_0); |
| | 0 | 354 | | generator.Emit(OpCodes.Ldarg_1); |
| | 0 | 355 | | generator.Emit(OpCodes.Callvirt, realMethod); |
| | 0 | 356 | | generator.Emit(OpCodes.Ret); |
| | | 357 | | |
| | 0 | 358 | | return dynamicMethod; |
| | 0 | 359 | | } |
| | | 360 | | |
| | | 361 | | public override Func<IEnumerable<TElement>, TCollection> CreateImmutableEnumerableCreateRangeDelegate<TCollectio |
| | 0 | 362 | | CreateDelegate<Func<IEnumerable<TElement>, TCollection>>( |
| | 0 | 363 | | CreateImmutableEnumerableCreateRangeDelegate(typeof(TCollection), typeof(TElement), typeof(IEnumerable<T |
| | | 364 | | |
| | | 365 | | private static DynamicMethod CreateImmutableEnumerableCreateRangeDelegate(Type collectionType, Type elementType, |
| | 0 | 366 | | { |
| | 0 | 367 | | MethodInfo realMethod = collectionType.GetImmutableEnumerableCreateRangeMethod(elementType); |
| | | 368 | | |
| | 0 | 369 | | var dynamicMethod = new DynamicMethod( |
| | 0 | 370 | | realMethod.Name, |
| | 0 | 371 | | collectionType, |
| | 0 | 372 | | new[] { enumerableType }, |
| | 0 | 373 | | typeof(ReflectionEmitMemberAccessor).Module, |
| | 0 | 374 | | skipVisibility: true); |
| | | 375 | | |
| | 0 | 376 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | | 377 | | |
| | 0 | 378 | | generator.Emit(OpCodes.Ldarg_0); |
| | 0 | 379 | | generator.Emit(OpCodes.Call, realMethod); |
| | 0 | 380 | | generator.Emit(OpCodes.Ret); |
| | | 381 | | |
| | 0 | 382 | | return dynamicMethod; |
| | 0 | 383 | | } |
| | | 384 | | |
| | | 385 | | public override Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection> CreateImmutableDictionaryCreateRangeD |
| | 0 | 386 | | CreateDelegate<Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection>>( |
| | 0 | 387 | | CreateImmutableDictionaryCreateRangeDelegate(typeof(TCollection), typeof(TKey), typeof(TValue), typeof(I |
| | | 388 | | |
| | | 389 | | private static DynamicMethod CreateImmutableDictionaryCreateRangeDelegate(Type collectionType, Type keyType, Typ |
| | 0 | 390 | | { |
| | 0 | 391 | | MethodInfo realMethod = collectionType.GetImmutableDictionaryCreateRangeMethod(keyType, valueType); |
| | | 392 | | |
| | 0 | 393 | | var dynamicMethod = new DynamicMethod( |
| | 0 | 394 | | realMethod.Name, |
| | 0 | 395 | | collectionType, |
| | 0 | 396 | | new[] { enumerableType }, |
| | 0 | 397 | | typeof(ReflectionEmitMemberAccessor).Module, |
| | 0 | 398 | | skipVisibility: true); |
| | | 399 | | |
| | 0 | 400 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | | 401 | | |
| | 0 | 402 | | generator.Emit(OpCodes.Ldarg_0); |
| | 0 | 403 | | generator.Emit(OpCodes.Call, realMethod); |
| | 0 | 404 | | generator.Emit(OpCodes.Ret); |
| | | 405 | | |
| | 0 | 406 | | return dynamicMethod; |
| | 0 | 407 | | } |
| | | 408 | | |
| | | 409 | | public override Func<object, TProperty> CreatePropertyGetter<TProperty>(PropertyInfo propertyInfo) => |
| | 2904 | 410 | | CreateDelegate<Func<object, TProperty>>(CreatePropertyGetter(propertyInfo, typeof(TProperty))); |
| | | 411 | | |
| | | 412 | | public override Func<TDeclaringType, TProperty> CreatePropertyGetter<TDeclaringType, TProperty>(PropertyInfo pro |
| | 0 | 413 | | CreateDelegate<Func<TDeclaringType, TProperty>>(CreatePropertyGetter(propertyInfo, typeof(TDeclaringType), t |
| | | 414 | | |
| | | 415 | | private static DynamicMethod CreatePropertyGetter(PropertyInfo propertyInfo, Type runtimePropertyType) |
| | 2904 | 416 | | { |
| | 2904 | 417 | | MethodInfo? realMethod = propertyInfo.GetMethod; |
| | 2904 | 418 | | Debug.Assert(realMethod != null); |
| | | 419 | | |
| | 2904 | 420 | | Type? declaringType = propertyInfo.DeclaringType; |
| | 2904 | 421 | | Debug.Assert(declaringType != null); |
| | | 422 | | |
| | 2904 | 423 | | Type declaredPropertyType = propertyInfo.PropertyType; |
| | | 424 | | |
| | 2904 | 425 | | DynamicMethod dynamicMethod = CreateGetterMethod(propertyInfo.Name, runtimePropertyType); |
| | 2904 | 426 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | | 427 | | |
| | 2904 | 428 | | generator.Emit(OpCodes.Ldarg_0); |
| | | 429 | | |
| | 2904 | 430 | | if (declaringType.IsValueType) |
| | 0 | 431 | | { |
| | 0 | 432 | | generator.Emit(OpCodes.Unbox, declaringType); |
| | 0 | 433 | | generator.Emit(OpCodes.Call, realMethod); |
| | 0 | 434 | | } |
| | | 435 | | else |
| | 2904 | 436 | | { |
| | 2904 | 437 | | generator.Emit(OpCodes.Castclass, declaringType); |
| | 2904 | 438 | | generator.Emit(OpCodes.Callvirt, realMethod); |
| | 2904 | 439 | | } |
| | | 440 | | |
| | | 441 | | // declaredPropertyType: Type of the property |
| | | 442 | | // runtimePropertyType: <T> of JsonConverter / JsonPropertyInfo |
| | | 443 | | |
| | 2904 | 444 | | if (declaredPropertyType != runtimePropertyType && declaredPropertyType.IsValueType) |
| | 0 | 445 | | { |
| | | 446 | | // Not supported scenario: possible if declaredPropertyType == int? and runtimePropertyType == int |
| | | 447 | | // We should catch that particular case earlier in converter generation. |
| | 0 | 448 | | Debug.Assert(!runtimePropertyType.IsValueType); |
| | | 449 | | |
| | 0 | 450 | | generator.Emit(OpCodes.Box, declaredPropertyType); |
| | 0 | 451 | | } |
| | | 452 | | |
| | 2904 | 453 | | generator.Emit(OpCodes.Ret); |
| | | 454 | | |
| | 2904 | 455 | | return dynamicMethod; |
| | 2904 | 456 | | } |
| | | 457 | | |
| | | 458 | | private static DynamicMethod CreatePropertyGetter(PropertyInfo propertyInfo, Type declaringType, Type runtimePro |
| | 0 | 459 | | { |
| | 0 | 460 | | MethodInfo? realMethod = propertyInfo.GetMethod; |
| | 0 | 461 | | Debug.Assert(realMethod != null); |
| | | 462 | | |
| | 0 | 463 | | Type? propertyDeclaringType = propertyInfo.DeclaringType; |
| | 0 | 464 | | Debug.Assert(propertyDeclaringType != null); |
| | 0 | 465 | | Debug.Assert(propertyDeclaringType.IsAssignableFrom(declaringType)); |
| | | 466 | | |
| | 0 | 467 | | Type declaredPropertyType = propertyInfo.PropertyType; |
| | | 468 | | |
| | 0 | 469 | | DynamicMethod dynamicMethod = CreateGetterMethod(propertyInfo.Name, declaringType, runtimePropertyType); |
| | 0 | 470 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | | 471 | | |
| | 0 | 472 | | if (declaringType.IsValueType) |
| | 0 | 473 | | { |
| | 0 | 474 | | generator.Emit(OpCodes.Ldarga_S, 0); |
| | 0 | 475 | | if (propertyDeclaringType.IsInterface) |
| | 0 | 476 | | { |
| | 0 | 477 | | generator.Emit(OpCodes.Constrained, declaringType); |
| | 0 | 478 | | generator.Emit(OpCodes.Callvirt, realMethod); |
| | 0 | 479 | | } |
| | | 480 | | else |
| | 0 | 481 | | { |
| | 0 | 482 | | generator.Emit(OpCodes.Call, realMethod); |
| | 0 | 483 | | } |
| | 0 | 484 | | } |
| | | 485 | | else |
| | 0 | 486 | | { |
| | 0 | 487 | | generator.Emit(OpCodes.Ldarg_0); |
| | | 488 | | |
| | 0 | 489 | | if (declaringType != propertyDeclaringType) |
| | 0 | 490 | | { |
| | 0 | 491 | | generator.Emit(OpCodes.Castclass, propertyDeclaringType); |
| | 0 | 492 | | } |
| | | 493 | | |
| | 0 | 494 | | generator.Emit(OpCodes.Callvirt, realMethod); |
| | 0 | 495 | | } |
| | | 496 | | |
| | 0 | 497 | | if (declaredPropertyType != runtimePropertyType && declaredPropertyType.IsValueType) |
| | 0 | 498 | | { |
| | 0 | 499 | | Debug.Assert(!runtimePropertyType.IsValueType); |
| | | 500 | | |
| | 0 | 501 | | generator.Emit(OpCodes.Box, declaredPropertyType); |
| | 0 | 502 | | } |
| | | 503 | | |
| | 0 | 504 | | generator.Emit(OpCodes.Ret); |
| | | 505 | | |
| | 0 | 506 | | return dynamicMethod; |
| | 0 | 507 | | } |
| | | 508 | | |
| | | 509 | | public override Action<object, TProperty> CreatePropertySetter<TProperty>(PropertyInfo propertyInfo) => |
| | 2904 | 510 | | CreateDelegate<Action<object, TProperty>>(CreatePropertySetter(propertyInfo, typeof(TProperty))); |
| | | 511 | | |
| | | 512 | | private static DynamicMethod CreatePropertySetter(PropertyInfo propertyInfo, Type runtimePropertyType) |
| | 2904 | 513 | | { |
| | 2904 | 514 | | MethodInfo? realMethod = propertyInfo.SetMethod; |
| | 2904 | 515 | | Debug.Assert(realMethod != null); |
| | | 516 | | |
| | 2904 | 517 | | Type? declaringType = propertyInfo.DeclaringType; |
| | 2904 | 518 | | Debug.Assert(declaringType != null); |
| | | 519 | | |
| | 2904 | 520 | | Type declaredPropertyType = propertyInfo.PropertyType; |
| | | 521 | | |
| | 2904 | 522 | | DynamicMethod dynamicMethod = CreateSetterMethod(propertyInfo.Name, runtimePropertyType); |
| | 2904 | 523 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | | 524 | | |
| | 2904 | 525 | | generator.Emit(OpCodes.Ldarg_0); |
| | 2904 | 526 | | generator.Emit(declaringType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, declaringType); |
| | 2904 | 527 | | generator.Emit(OpCodes.Ldarg_1); |
| | | 528 | | |
| | | 529 | | // declaredPropertyType: Type of the property |
| | | 530 | | // runtimePropertyType: <T> of JsonConverter / JsonPropertyInfo |
| | | 531 | | |
| | 2904 | 532 | | if (declaredPropertyType != runtimePropertyType && declaredPropertyType.IsValueType) |
| | 0 | 533 | | { |
| | | 534 | | // Not supported scenario: possible if e.g. declaredPropertyType == int? and runtimePropertyType == int |
| | | 535 | | // We should catch that particular case earlier in converter generation. |
| | 0 | 536 | | Debug.Assert(!runtimePropertyType.IsValueType); |
| | | 537 | | |
| | 0 | 538 | | generator.Emit(OpCodes.Unbox_Any, declaredPropertyType); |
| | 0 | 539 | | } |
| | | 540 | | |
| | 2904 | 541 | | generator.Emit(declaringType.IsValueType ? OpCodes.Call : OpCodes.Callvirt, realMethod); |
| | 2904 | 542 | | generator.Emit(OpCodes.Ret); |
| | | 543 | | |
| | 2904 | 544 | | return dynamicMethod; |
| | 2904 | 545 | | } |
| | | 546 | | |
| | | 547 | | public override Func<object, TProperty> CreateFieldGetter<TProperty>(FieldInfo fieldInfo) => |
| | 0 | 548 | | CreateDelegate<Func<object, TProperty>>(CreateFieldGetter(fieldInfo, typeof(TProperty))); |
| | | 549 | | |
| | | 550 | | private static DynamicMethod CreateFieldGetter(FieldInfo fieldInfo, Type runtimeFieldType) |
| | 0 | 551 | | { |
| | 0 | 552 | | Type? declaringType = fieldInfo.DeclaringType; |
| | 0 | 553 | | Debug.Assert(declaringType != null); |
| | | 554 | | |
| | 0 | 555 | | Type declaredFieldType = fieldInfo.FieldType; |
| | | 556 | | |
| | 0 | 557 | | DynamicMethod dynamicMethod = CreateGetterMethod(fieldInfo.Name, runtimeFieldType); |
| | 0 | 558 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | | 559 | | |
| | 0 | 560 | | generator.Emit(OpCodes.Ldarg_0); |
| | 0 | 561 | | generator.Emit( |
| | 0 | 562 | | declaringType.IsValueType |
| | 0 | 563 | | ? OpCodes.Unbox |
| | 0 | 564 | | : OpCodes.Castclass, |
| | 0 | 565 | | declaringType); |
| | 0 | 566 | | generator.Emit(OpCodes.Ldfld, fieldInfo); |
| | | 567 | | |
| | | 568 | | // declaredFieldType: Type of the field |
| | | 569 | | // runtimeFieldType: <T> of JsonConverter / JsonPropertyInfo |
| | | 570 | | |
| | 0 | 571 | | if (declaredFieldType.IsValueType && declaredFieldType != runtimeFieldType) |
| | 0 | 572 | | { |
| | 0 | 573 | | generator.Emit(OpCodes.Box, declaredFieldType); |
| | 0 | 574 | | } |
| | | 575 | | |
| | 0 | 576 | | generator.Emit(OpCodes.Ret); |
| | | 577 | | |
| | 0 | 578 | | return dynamicMethod; |
| | 0 | 579 | | } |
| | | 580 | | |
| | | 581 | | public override Action<object, TProperty> CreateFieldSetter<TProperty>(FieldInfo fieldInfo) => |
| | 0 | 582 | | CreateDelegate<Action<object, TProperty>>(CreateFieldSetter(fieldInfo, typeof(TProperty))); |
| | | 583 | | |
| | | 584 | | private static DynamicMethod CreateFieldSetter(FieldInfo fieldInfo, Type runtimeFieldType) |
| | 0 | 585 | | { |
| | 0 | 586 | | Type? declaringType = fieldInfo.DeclaringType; |
| | 0 | 587 | | Debug.Assert(declaringType != null); |
| | | 588 | | |
| | 0 | 589 | | Type declaredFieldType = fieldInfo.FieldType; |
| | | 590 | | |
| | 0 | 591 | | DynamicMethod dynamicMethod = CreateSetterMethod(fieldInfo.Name, runtimeFieldType); |
| | 0 | 592 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | | 593 | | |
| | 0 | 594 | | generator.Emit(OpCodes.Ldarg_0); |
| | 0 | 595 | | generator.Emit(declaringType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, declaringType); |
| | 0 | 596 | | generator.Emit(OpCodes.Ldarg_1); |
| | | 597 | | |
| | | 598 | | // declaredFieldType: Type of the field |
| | | 599 | | // runtimeFieldType: <T> of JsonConverter / JsonPropertyInfo |
| | | 600 | | |
| | 0 | 601 | | if (declaredFieldType != runtimeFieldType && declaredFieldType.IsValueType) |
| | 0 | 602 | | { |
| | 0 | 603 | | generator.Emit(OpCodes.Unbox_Any, declaredFieldType); |
| | 0 | 604 | | } |
| | | 605 | | |
| | 0 | 606 | | generator.Emit(OpCodes.Stfld, fieldInfo); |
| | 0 | 607 | | generator.Emit(OpCodes.Ret); |
| | | 608 | | |
| | 0 | 609 | | return dynamicMethod; |
| | 0 | 610 | | } |
| | | 611 | | |
| | | 612 | | public override UnionTryGetValueAccessor<TUnion> CreateUnionTryGetValueAccessor<TUnion>(IReadOnlyList<KeyValuePa |
| | 0 | 613 | | CreateDelegate<UnionTryGetValueAccessor<TUnion>>(CreateUnionTryGetValueAccessor(typeof(TUnion), entries)); |
| | | 614 | | |
| | | 615 | | private static DynamicMethod CreateUnionTryGetValueAccessor(Type unionType, IReadOnlyList<KeyValuePair<Type, Met |
| | 0 | 616 | | { |
| | 0 | 617 | | Debug.Assert(entries.Count > 0, "Should not build a chained accessor for an empty entry list."); |
| | | 618 | | |
| | | 619 | | // Emit a single DynamicMethod that chains the user-declared |
| | | 620 | | // 'bool TUnion.TryGetValue(out CaseType)' overloads in caller-supplied order. |
| | | 621 | | // First successful overload sets the out parameters and returns true; if none |
| | | 622 | | // match, the trailer writes null/null and returns false. Producing one piece of |
| | | 623 | | // IL avoids per-entry closure or delegate hop and keeps the dispatch O(chainLength) |
| | | 624 | | // with no managed allocations on the hot path. |
| | 0 | 625 | | DynamicMethod dynamicMethod = new DynamicMethod( |
| | 0 | 626 | | "UnionTryGetValueAccessor", |
| | 0 | 627 | | typeof(bool), |
| | 0 | 628 | | new[] { unionType, typeof(Type).MakeByRefType(), typeof(object).MakeByRefType() }, |
| | 0 | 629 | | typeof(ReflectionEmitMemberAccessor).Module, |
| | 0 | 630 | | skipVisibility: true); |
| | | 631 | | |
| | 0 | 632 | | ILGenerator generator = dynamicMethod.GetILGenerator(); |
| | 0 | 633 | | bool unionIsValueType = unionType.IsValueType; |
| | | 634 | | |
| | 0 | 635 | | for (int i = 0; i < entries.Count; i++) |
| | 0 | 636 | | { |
| | 0 | 637 | | KeyValuePair<Type, MethodInfo> entry = entries[i]; |
| | 0 | 638 | | Type caseType = entry.Key; |
| | 0 | 639 | | MethodInfo method = entry.Value; |
| | 0 | 640 | | LocalBuilder extracted = generator.DeclareLocal(caseType); |
| | 0 | 641 | | Label nextEntry = generator.DefineLabel(); |
| | | 642 | | |
| | | 643 | | // if (!union.TryGetValue(out extracted)) goto nextEntry; |
| | 0 | 644 | | if (unionIsValueType) |
| | 0 | 645 | | { |
| | 0 | 646 | | generator.Emit(OpCodes.Ldarga_S, (byte)0); |
| | 0 | 647 | | generator.Emit(OpCodes.Ldloca_S, extracted); |
| | 0 | 648 | | generator.Emit(OpCodes.Call, method); |
| | 0 | 649 | | } |
| | | 650 | | else |
| | 0 | 651 | | { |
| | 0 | 652 | | generator.Emit(OpCodes.Ldarg_0); |
| | 0 | 653 | | generator.Emit(OpCodes.Ldloca_S, extracted); |
| | 0 | 654 | | generator.Emit(OpCodes.Callvirt, method); |
| | 0 | 655 | | } |
| | 0 | 656 | | generator.Emit(OpCodes.Brfalse, nextEntry); |
| | | 657 | | |
| | | 658 | | // caseType = typeof(CaseType); |
| | 0 | 659 | | generator.Emit(OpCodes.Ldarg_1); |
| | 0 | 660 | | generator.Emit(OpCodes.Ldtoken, caseType); |
| | 0 | 661 | | generator.Emit(OpCodes.Call, s_getTypeFromHandleMethod); |
| | 0 | 662 | | generator.Emit(OpCodes.Stind_Ref); |
| | | 663 | | |
| | | 664 | | // value = (object?)extracted; |
| | 0 | 665 | | generator.Emit(OpCodes.Ldarg_2); |
| | 0 | 666 | | generator.Emit(OpCodes.Ldloc, extracted); |
| | 0 | 667 | | if (caseType.IsValueType) |
| | 0 | 668 | | { |
| | 0 | 669 | | generator.Emit(OpCodes.Box, caseType); |
| | 0 | 670 | | } |
| | 0 | 671 | | generator.Emit(OpCodes.Stind_Ref); |
| | | 672 | | |
| | | 673 | | // return true; |
| | 0 | 674 | | generator.Emit(OpCodes.Ldc_I4_1); |
| | 0 | 675 | | generator.Emit(OpCodes.Ret); |
| | | 676 | | |
| | 0 | 677 | | generator.MarkLabel(nextEntry); |
| | 0 | 678 | | } |
| | | 679 | | |
| | | 680 | | // No overload matched: caseType = null; value = null; return false; |
| | 0 | 681 | | generator.Emit(OpCodes.Ldarg_1); |
| | 0 | 682 | | generator.Emit(OpCodes.Ldnull); |
| | 0 | 683 | | generator.Emit(OpCodes.Stind_Ref); |
| | 0 | 684 | | generator.Emit(OpCodes.Ldarg_2); |
| | 0 | 685 | | generator.Emit(OpCodes.Ldnull); |
| | 0 | 686 | | generator.Emit(OpCodes.Stind_Ref); |
| | 0 | 687 | | generator.Emit(OpCodes.Ldc_I4_0); |
| | 0 | 688 | | generator.Emit(OpCodes.Ret); |
| | | 689 | | |
| | 0 | 690 | | return dynamicMethod; |
| | 0 | 691 | | } |
| | | 692 | | |
| | 0 | 693 | | private static readonly MethodInfo s_getTypeFromHandleMethod = |
| | 0 | 694 | | typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle), new[] { typeof(RuntimeTypeHandle) })!; |
| | | 695 | | |
| | | 696 | | private static DynamicMethod CreateGetterMethod(string memberName, Type memberType) => |
| | 2904 | 697 | | new DynamicMethod( |
| | 2904 | 698 | | memberName + "Getter", |
| | 2904 | 699 | | memberType, |
| | 2904 | 700 | | new[] { typeof(object) }, |
| | 2904 | 701 | | typeof(ReflectionEmitMemberAccessor).Module, |
| | 2904 | 702 | | skipVisibility: true); |
| | | 703 | | |
| | | 704 | | private static DynamicMethod CreateGetterMethod(string memberName, Type declaringType, Type memberType) => |
| | 0 | 705 | | new DynamicMethod( |
| | 0 | 706 | | memberName + "Getter", |
| | 0 | 707 | | memberType, |
| | 0 | 708 | | new[] { declaringType }, |
| | 0 | 709 | | typeof(ReflectionEmitMemberAccessor).Module, |
| | 0 | 710 | | skipVisibility: true); |
| | | 711 | | |
| | | 712 | | private static DynamicMethod CreateSetterMethod(string memberName, Type memberType) => |
| | 2904 | 713 | | new DynamicMethod( |
| | 2904 | 714 | | memberName + "Setter", |
| | 2904 | 715 | | typeof(void), |
| | 2904 | 716 | | new[] { typeof(object), memberType }, |
| | 2904 | 717 | | typeof(ReflectionEmitMemberAccessor).Module, |
| | 2904 | 718 | | skipVisibility: true); |
| | | 719 | | |
| | | 720 | | [return: NotNullIfNotNull(nameof(method))] |
| | | 721 | | private static T? CreateDelegate<T>(DynamicMethod? method) where T : Delegate => |
| | 6798 | 722 | | (T?)method?.CreateDelegate(typeof(T)); |
| | | 723 | | } |
| | | 724 | | } |
| | | 725 | | #endif |
| | | 726 | | |