|
| 1 | +--- |
| 2 | +title: "Breaking change - Casting IDispatchEx COM object to IReflect fails" |
| 3 | +description: "Learn about the breaking change in .NET 10 where casting a COM object that implements IDispatchEx to IReflect now fails." |
| 4 | +ms.date: 11/17/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +--- |
| 7 | + |
| 8 | +# Casting COM object that implements IDispatchEx to IReflect fails |
| 9 | + |
| 10 | +Since .NET 5, it was possible to cast a COM object that implements [IDispatchEx](/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/reference/idispatchex-interface) to <xref:System.Reflection.IReflect>. However, all members on that `IReflect` instance threw <xref:System.TypeLoadException>. Starting in .NET 10, this behavior changed and the cast now fails. |
| 11 | + |
| 12 | +## Version introduced |
| 13 | + |
| 14 | +.NET 10 |
| 15 | + |
| 16 | +## Previous behavior |
| 17 | + |
| 18 | +Previously, casting a COM object that implements `IDispatchEx` to `IReflect` succeeded. |
| 19 | + |
| 20 | +```csharp |
| 21 | +using System.Reflection; |
| 22 | +var file = Activator.CreateInstance(Type.GetTypeFromProgID("htmlfile")); |
| 23 | +Console.WriteLine("IReflect is " + (file is IReflect ? "supported" : "NOT supported")); |
| 24 | +// Printed "IReflect is supported". |
| 25 | +``` |
| 26 | + |
| 27 | +## New behavior |
| 28 | + |
| 29 | +Starting in .NET 10, casting a COM object that implements `IDispatchEx` to `IReflect` fails. |
| 30 | + |
| 31 | +```csharp |
| 32 | +using System.Reflection; |
| 33 | +var file = Activator.CreateInstance(Type.GetTypeFromProgID("htmlfile")); |
| 34 | +Console.WriteLine("IReflect is " + (file is IReflect ? "supported" : "NOT supported")); |
| 35 | +// Prints "IReflect is NOT supported". |
| 36 | +``` |
| 37 | + |
| 38 | +## Type of breaking change |
| 39 | + |
| 40 | +This change is a [behavioral change](../../categories.md#behavioral-change). |
| 41 | + |
| 42 | +## Reason for change |
| 43 | + |
| 44 | +This capability was removed because all members on the resulting `IReflect` instance were unusable. Also, the <xref:System.TypeLoadException> exception that resulted from accessing any of the members mentioned a type that was never included in .NET and was therefore confusing and unhelpful as to the underlying issue. |
| 45 | + |
| 46 | +## Recommended action |
| 47 | + |
| 48 | +The only viable question that could be asked in .NET 5+ with this cast was, "Does the type implement IDispatchEx?" In this case, a better way to ask that question is: |
| 49 | + |
| 50 | +```csharp |
| 51 | +var file = Activator.CreateInstance(Type.GetTypeFromProgID("htmlfile")); |
| 52 | +Console.WriteLine("IDispatchEx is " + (file is IDispatchEx ? "supported" : "NOT supported")); |
| 53 | + |
| 54 | +[ComImport] |
| 55 | +[Guid("A6EF9860-C720-11D0-9337-00A0C90DCAA9")] |
| 56 | +interface IDispatchEx { } |
| 57 | +``` |
| 58 | + |
| 59 | +## Affected APIs |
| 60 | + |
| 61 | +None. |
0 commit comments