Skip to content

Commit

Permalink
Add test case for IDynamicInterfaceCastable on ValueTypes. (#103849)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronRobinsonMSFT authored Jun 22, 2024
1 parent ce1ae77 commit 386f16f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/tests/Interop/IDynamicInterfaceCastable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -334,6 +335,15 @@ int ITest.GetNumber()
}
}

public struct ValueTypeDynamicInterfaceCastable : IDynamicInterfaceCastable
{
public bool IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented)
=> throw new UnreachableException("ValueType implementations are ignored");

public RuntimeTypeHandle GetInterfaceImplementation(RuntimeTypeHandle interfaceType)
=> throw new UnreachableException("ValueType implementations are ignored");
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/55742", TestRuntimes.Mono)]
public class Program
{
Expand Down Expand Up @@ -565,5 +575,26 @@ public static void ValidateErrorHandling()
ex = Assert.Throws<InvalidCastException>(() => testObj.GetMyType());
Console.WriteLine($" ---- {ex.GetType().Name}: {ex.Message}");
}

[Fact]
public static void ValidateValueTypeImplementationIgnored()
{
Console.WriteLine($"Running {nameof(ValidateValueTypeImplementationIgnored)}");

Console.WriteLine(" -- Validate casting is ignored");
object notCastableVC = Create();

// Confirm the ValueType implements IDynamicInterfaceCastable
Assert.True(notCastableVC.GetType().IsValueType);
Assert.True(notCastableVC is IDynamicInterfaceCastable);

// Confirm the IDynamicInterfaceCastable implementation isn't called.
Assert.False(notCastableVC is ITest);
Assert.Null(notCastableVC as ITest);
Assert.Throws<InvalidCastException>(() => { var testObj = (ITest)notCastableVC; });

[MethodImpl(MethodImplOptions.NoInlining)]
static object Create() => (object)new ValueTypeDynamicInterfaceCastable();
}
}
}

0 comments on commit 386f16f

Please sign in to comment.