From 35cf2750c73bf0a44c5ad65d6f1faeea7bde13bc Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 14 Sep 2018 11:27:19 -0700 Subject: [PATCH] Fix .NET adapter to not throw when fails to create a PSMethod due to ByRef-like type --- .../engine/MshMemberInfo.cs | 18 +++++++++--------- .../Interop/DotNet/DotNetInterop.Tests.ps1 | 12 ++++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/System.Management.Automation/engine/MshMemberInfo.cs b/src/System.Management.Automation/engine/MshMemberInfo.cs index f04f1e44069..5a1d95b7112 100644 --- a/src/System.Management.Automation/engine/MshMemberInfo.cs +++ b/src/System.Management.Automation/engine/MshMemberInfo.cs @@ -2691,18 +2691,18 @@ private static Type GetMethodGroupType(MethodInfo methodInfo) return typeof(Func); } - var methodTypes = new Type[parameterInfos.Length + 1]; - for (int i = 0; i < parameterInfos.Length; i++) + try { - var parameterInfo = parameterInfos[i]; - Type parameterType = parameterInfo.ParameterType; - methodTypes[i] = GetPSMethodProjectedType(parameterType, parameterInfo.IsOut); - } + 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); + methodTypes[parameterInfos.Length] = GetPSMethodProjectedType(methodInfo.ReturnType); - try - { 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 fd85105b5cf..a2c1ff6944f 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 {