Skip to content

Commit 386f16f

Browse files
Add test case for IDynamicInterfaceCastable on ValueTypes. (#103849)
1 parent ce1ae77 commit 386f16f

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

  • src/tests/Interop/IDynamicInterfaceCastable

src/tests/Interop/IDynamicInterfaceCastable/Program.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.Runtime.CompilerServices;
78
using System.Runtime.InteropServices;
89

@@ -334,6 +335,15 @@ int ITest.GetNumber()
334335
}
335336
}
336337

338+
public struct ValueTypeDynamicInterfaceCastable : IDynamicInterfaceCastable
339+
{
340+
public bool IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented)
341+
=> throw new UnreachableException("ValueType implementations are ignored");
342+
343+
public RuntimeTypeHandle GetInterfaceImplementation(RuntimeTypeHandle interfaceType)
344+
=> throw new UnreachableException("ValueType implementations are ignored");
345+
}
346+
337347
[ActiveIssue("https://github.com/dotnet/runtime/issues/55742", TestRuntimes.Mono)]
338348
public class Program
339349
{
@@ -565,5 +575,26 @@ public static void ValidateErrorHandling()
565575
ex = Assert.Throws<InvalidCastException>(() => testObj.GetMyType());
566576
Console.WriteLine($" ---- {ex.GetType().Name}: {ex.Message}");
567577
}
578+
579+
[Fact]
580+
public static void ValidateValueTypeImplementationIgnored()
581+
{
582+
Console.WriteLine($"Running {nameof(ValidateValueTypeImplementationIgnored)}");
583+
584+
Console.WriteLine(" -- Validate casting is ignored");
585+
object notCastableVC = Create();
586+
587+
// Confirm the ValueType implements IDynamicInterfaceCastable
588+
Assert.True(notCastableVC.GetType().IsValueType);
589+
Assert.True(notCastableVC is IDynamicInterfaceCastable);
590+
591+
// Confirm the IDynamicInterfaceCastable implementation isn't called.
592+
Assert.False(notCastableVC is ITest);
593+
Assert.Null(notCastableVC as ITest);
594+
Assert.Throws<InvalidCastException>(() => { var testObj = (ITest)notCastableVC; });
595+
596+
[MethodImpl(MethodImplOptions.NoInlining)]
597+
static object Create() => (object)new ValueTypeDynamicInterfaceCastable();
598+
}
568599
}
569600
}

0 commit comments

Comments
 (0)