CreateObjectFlags.Unwrap only unwraps on target instance

Previously, if you called GetOrCreateObjectForComInstance(IntPtr, CreateObjectFlags) on a ComWrappers instance with the CreateObjectFlags.Unwrap flag, a managed object wrapper was unwrapped from any ComWrappers instance. Now when the flag is specified, only wrappers from the ComWrappers instance that GetOrCreateObjectFromComInstance was called on are unwrapped.

The Unwrap flag was the only API that reached "across" ComWrappers instances, so its behavior was unintuitive. Additionally, the new ComWrappers.TryGetObject(IntPtr, Object) API is available to unwrap a COM object from any ComWrappers instance.

Previous behavior

Calling GetOrCreateObjectForComInstance(IntPtr, CreateObjectFlags) on a ComWrappers instance with the CreateObjectFlags.Unwrap flag unwrapped a managed object wrapper from any ComWrappers instance.

New behavior

Calling GetOrCreateObjectForComInstance(IntPtr, CreateObjectFlags) on a ComWrappers instance with the CreateObjectFlags.Unwrap flag only unwraps a managed object wrapper from the ComWrappers instance that GetOrCreateObjectForComInstance was called on. If given a wrapper from a different ComWrappers instance, the ComWrappers instance creates a new wrapper.

Version introduced

.NET 8 Preview 5

Type of breaking change

This change is a behavioral change.

Reason for change

The previous behavior was unintuitive. It also broke the encapsulation experience where developers can define how COM interop works for their code by using their own custom ComWrappers instances.

If you want to keep the previous behavior, call ComWrappers.TryGetObject(IntPtr, Object) before calling GetOrCreateObjectForComInstance(IntPtr, CreateObjectFlags).

Affected APIs