Fix .NET adapter to not throw when fails to create a PSMethod due to ByRef-like type (#7788)

This commit is contained in:
Dongbo Wang
2018-09-17 12:45:55 -07:00
committed by GitHub
parent f90fa8afc1
commit 285e471034
2 changed files with 22 additions and 10 deletions
@@ -2691,18 +2691,18 @@ namespace System.Management.Automation
return typeof(Func<PSNonBindableType>);
}
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)
@@ -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 {