@@ -19,6 +19,7 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1919 using System . Reflection . Emit ;
2020
2121 using Castle . DynamicProxy . Internal ;
22+ using Castle . DynamicProxy . Tokens ;
2223
2324 internal class ReferencesToObjectArrayExpression : IExpression
2425 {
@@ -47,11 +48,16 @@ public void Emit(ILGenerator gen)
4748#if FEATURE_BYREFLIKE
4849 if ( reference . Type . IsByRefLikeSafe ( ) )
4950 {
50- // The by-ref-like argument value cannot be put into the `object[]` array,
51- // because it cannot be boxed. We need to replace it with some other value.
52-
53- // For now, we just erase it by substituting `null`:
54- gen . Emit ( OpCodes . Ldnull ) ;
51+ // By-ref-like arguments cannot be put directly into the `object[]` array,
52+ // because they cannot live on the heap. So instead we are taking their address,
53+ // putting that in a `System.Reflection.Pointer`, and then store *that*:
54+ var localCopy = gen . DeclareLocal ( reference . Type ) ;
55+ ArgumentsUtil . EmitLoadOwnerAndReference ( reference , gen ) ;
56+ gen . Emit ( OpCodes . Stloc , localCopy ) ;
57+ gen . Emit ( OpCodes . Ldloca , localCopy ) ;
58+ gen . Emit ( OpCodes . Ldtoken , reference . Type . MakePointerType ( ) ) ;
59+ gen . Emit ( OpCodes . Call , TypeMethods . GetTypeFromHandle ) ;
60+ gen . Emit ( OpCodes . Call , PointerMethods . BoxMethod ) ;
5561 gen . Emit ( OpCodes . Stelem_Ref ) ;
5662
5763 continue ;
0 commit comments