Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7201,6 +7201,13 @@ internal static Expression InvokeMethod(MethodBase mi, DynamicMetaObject target,
var argValue = parameters[i].DefaultValue;
if (argValue == null)
{
if (parameterType.IsByRef)
{
// When the default value is null for a ByRef parameter (e.g. an optional `in` parameter
// using `default`), expression trees cannot create Expression.Default for the T& type.
// In that case we switch to the element type and use Default(TElement) instead.
parameterType = parameterType.GetElementType();
}
argExprs[i] = Expression.Default(parameterType);
}
else if (!parameters[i].HasDefaultValue && parameterType != typeof(object) && argValue == Type.Missing)
Expand Down
21 changes: 21 additions & 0 deletions test/powershell/engine/Basic/CLRBinding.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public class TestClass
public static string StaticWithOptionalExpected() => StaticWithOptional();
public static string StaticWithOptional([Optional] string value) => value;

public static int PrimitiveTypeWithInDefault(in int value = default) => value;

public static Guid ValueTypeWithInDefault(in Guid value = default) => value;

public static string RefTypeWithInDefault(in string value = default) => value;

public object InstanceWithDefaultExpected() => InstanceWithDefault();
public object InstanceWithDefault(object value = null) => value;

Expand Down Expand Up @@ -101,6 +107,21 @@ public class TestClassCstorWithOptional
$actual | Should -Be $expected
}

It "Binds to static method with primitive type with in modifier and default argument" {
$actual = [CLRBindingTests.TestClass]::PrimitiveTypeWithInDefault()
$actual | Should -Be 0
}

It "Binds to static method with value type with in modifier and default argument" {
$actual = [CLRBindingTests.TestClass]::ValueTypeWithInDefault()
$actual | Should -Be ([Guid]::Empty)
}

It "Binds to static method with ref type with in modifier and default argument" {
$actual = [CLRBindingTests.TestClass]::RefTypeWithInDefault()
$null -eq $actual | Should -BeTrue
}

It "Binds to instance method with default argument" {
$c = [CLRBindingTests.TestClass]::new()

Expand Down
Loading