From 285e47103448c153bc09e13f8c142c98b59115f7 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 17 Sep 2018 12:45:55 -0700 Subject: [PATCH] Fix .NET adapter to not throw when fails to create a PSMethod due to ByRef-like type (#7788) --- .../engine/MshMemberInfo.cs | 20 +++++++++---------- .../Interop/DotNet/DotNetInterop.Tests.ps1 | 12 +++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/System.Management.Automation/engine/MshMemberInfo.cs b/src/System.Management.Automation/engine/MshMemberInfo.cs index f04f1e4406..5a1d95b711 100644 --- a/src/System.Management.Automation/engine/MshMemberInfo.cs +++ b/src/System.Management.Automation/engine/MshMemberInfo.cs @@ -2691,18 +2691,18 @@ namespace System.Management.Automation return typeof(Func); } - var methodTypes = new Type[parameterInfos.Length + 1]; - for (int i = 0; i < parameterInfos.Length; i++) - { - var parameterInfo = parameterInfos[i]; - Type parameterType = parameterInfo.ParameterType; - methodTypes[i] = GetPSMethodProjectedType(parameterType, parameterInfo.IsOut); - } - - methodTypes[parameterInfos.Length] = GetPSMethodProjectedType(methodInfo.ReturnType); - try { + var methodTypes = new Type[parameterInfos.Length + 1]; + for (int i = 0; i < parameterInfos.Length; i++) + { + var parameterInfo = parameterInfos[i]; + Type parameterType = parameterInfo.ParameterType; + methodTypes[i] = GetPSMethodProjectedType(parameterType, parameterInfo.IsOut); + } + + methodTypes[parameterInfos.Length] = GetPSMethodProjectedType(methodInfo.ReturnType); + return DelegateHelpers.MakeDelegate(methodTypes); } catch (TypeLoadException) diff --git a/test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1 b/test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1 index fd85105b5c..a2c1ff6944 100644 --- a/test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1 +++ b/test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1 @@ -92,6 +92,13 @@ namespace DotNetInterop public MyByRefLikeType(int i) { } public static int Index; } + + public class ExampleProblemClass + { + public void ProblemMethod(ref MyByRefLikeType value) + { + } + } } '@ if (-not ("DotNetInterop.Test" -as [type])) @@ -229,6 +236,11 @@ namespace DotNetInterop { $testObj[1] = 1 } | Should -Throw -ErrorId "CannotIndexWithByRefLikeReturnType" } + It "Create instance of type with method that use a ByRef-like type as a ByRef parameter" { + $obj = [DotNetInterop.ExampleProblemClass]::new() + $obj | Should -BeOfType DotNetInterop.ExampleProblemClass + } + Context "Passing value that is implicitly/explicitly castable to ByRef-like parameter in method invocation" { BeforeAll {