mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fix .NET adapter to not throw when fails to create a PSMethod due to ByRef-like type (#7788)
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user