|
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
6 | 7 | using System.Runtime.CompilerServices; |
7 | 8 | using System.Runtime.InteropServices; |
8 | 9 |
|
@@ -334,6 +335,15 @@ int ITest.GetNumber() |
334 | 335 | } |
335 | 336 | } |
336 | 337 |
|
| 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 | + |
337 | 347 | [ActiveIssue("https://github.com/dotnet/runtime/issues/55742", TestRuntimes.Mono)] |
338 | 348 | public class Program |
339 | 349 | { |
@@ -565,5 +575,26 @@ public static void ValidateErrorHandling() |
565 | 575 | ex = Assert.Throws<InvalidCastException>(() => testObj.GetMyType()); |
566 | 576 | Console.WriteLine($" ---- {ex.GetType().Name}: {ex.Message}"); |
567 | 577 | } |
| 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 | + } |
568 | 599 | } |
569 | 600 | } |
0 commit comments