From 486757d783a9769a44080e11f83c290efa401716 Mon Sep 17 00:00:00 2001 From: Wei Zheng <13881045+wzchua@users.noreply.github.com> Date: Mon, 12 Apr 2021 18:25:24 +0800 Subject: [PATCH] Fix DispatchProxy not working with in parameters (#49214) * Fix DispatchProxy not working with in parameters * Correct typo Co-authored-by: Christopher Watford * Fixed Calling Convention Removed unneeded mods Co-authored-by: Christopher Watford --- .../System/Reflection/DispatchProxyGenerator.cs | 14 ++++++++++++-- .../tests/DispatchProxyTests.cs | 1 + .../tests/TestTypes.cs | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs index 6cb945ca3a382..a9cd9f7c17ccf 100644 --- a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs +++ b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs @@ -394,9 +394,19 @@ internal void AddInterfaceImpl([DynamicallyAccessedMembers(DynamicallyAccessedMe private MethodBuilder AddMethodImpl(MethodInfo mi, int methodInfoIndex) { ParameterInfo[] parameters = mi.GetParameters(); - Type[] paramTypes = ParamTypes(parameters, false); + Type[] paramTypes = new Type[parameters.Length]; + Type[][] paramReqMods = new Type[paramTypes.Length][]; + + for (int i = 0; i < parameters.Length; i++) + { + paramTypes[i] = parameters[i].ParameterType; + paramReqMods[i] = parameters[i].GetRequiredCustomModifiers(); + } + + MethodBuilder mdb = _tb.DefineMethod(mi.Name, MethodAttributes.Public | MethodAttributes.Virtual, CallingConventions.Standard, + mi.ReturnType, null, null, + paramTypes, paramReqMods, null); - MethodBuilder mdb = _tb.DefineMethod(mi.Name, MethodAttributes.Public | MethodAttributes.Virtual, mi.ReturnType, paramTypes); if (mi.ContainsGenericParameters) { Type[] ts = mi.GetGenericArguments(); diff --git a/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs b/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs index dd0563bf4800b..cb5edc26d5d5f 100644 --- a/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs +++ b/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs @@ -603,6 +603,7 @@ public static void Invoke_Ref_Out_In_Method() testRefOutInInvocation(p => p.Out(out _), null); testRefOutInInvocation(p => p.OutAttribute(value), "Hello"); testRefOutInInvocation(p => p.Ref(ref value), "Hello"); + testRefOutInInvocation(p => p.In(in value), "Hello"); } private static void testRefOutInInvocation(Action invocation, string expected) diff --git a/src/libraries/System.Reflection.DispatchProxy/tests/TestTypes.cs b/src/libraries/System.Reflection.DispatchProxy/tests/TestTypes.cs index d4fa174fd2981..dca9f2d266c8d 100644 --- a/src/libraries/System.Reflection.DispatchProxy/tests/TestTypes.cs +++ b/src/libraries/System.Reflection.DispatchProxy/tests/TestTypes.cs @@ -19,6 +19,7 @@ public interface TestType_IHelloService public interface TestType_IOut_Ref { + void In(in string message); void Out(out string message); void Ref(ref string message); void InAttribute([In] string message);