diff --git a/src/Tests/FunctionalTests/DynamicInterfaceCasting/Program.cs b/src/Tests/FunctionalTests/DynamicInterfaceCasting/Program.cs index 5f23cec16..f026e2d67 100644 --- a/src/Tests/FunctionalTests/DynamicInterfaceCasting/Program.cs +++ b/src/Tests/FunctionalTests/DynamicInterfaceCasting/Program.cs @@ -38,6 +38,17 @@ IList> list2 = new List>(); instance2.IterableOfPointIterablesProperty = list2; +// Ensure that these don't crash but return null +if ((IWinRTObject)instance as IList != null) +{ + return 105; +} + +if ((IWinRTObject)instance as IList != null) +{ + return 106; +} + return 100; object SetAndGetBoxedValue(TestComponentCSharp.Class instance, object val) @@ -45,3 +56,8 @@ object SetAndGetBoxedValue(TestComponentCSharp.Class instance, object val) instance.ObjectProperty = val; return instance.ObjectProperty; } + +class ManagedClass +{ + public int Number { get; set; } +} \ No newline at end of file diff --git a/src/WinRT.Runtime/IWinRTObject.net5.cs b/src/WinRT.Runtime/IWinRTObject.net5.cs index faf4e4481..c75f85972 100644 --- a/src/WinRT.Runtime/IWinRTObject.net5.cs +++ b/src/WinRT.Runtime/IWinRTObject.net5.cs @@ -159,6 +159,13 @@ internal sealed bool IsInterfaceImplementedFallback(RuntimeTypeHandle interfaceT } } + // Make sure we are going to do a QI on a WinRT interface, + // otherwise we can get a helper type for a non WinRT type. + if (!Projections.IsTypeWindowsRuntimeType(type)) + { + return false; + } + Type helperType = type.FindHelperType(); if (helperType is null || !helperType.IsInterface) {