// IDispose public void Dispose() { // we may have recicular COM references to itself // e.g., via _baseIComInterface // make sure to release all references if (_baseIComInterface != null) { Marshal.ReleaseComObject(_baseIComInterface); _baseIComInterface = null; } if (_baseClassComProxy != null) { _baseClassComProxy.Dispose(); _baseClassComProxy = null; } }
// constructor public ImprovedClass() { // get OldLibrary.BaseClass.IComInterface via reflection var typeBaseIComInterface = typeof(OldLibrary.BaseClass).GetInterfaces().First((t) => t.GUID == typeof(NewLibrary.IComInterface).GUID); // get the COM interface for OldLibrary.BaseClass.IComInterface _unkBaseIComInterface = Marshal.GetComInterfaceForObject(this, typeBaseIComInterface, CustomQueryInterfaceMode.Ignore); Marshal.AddRef(_unkBaseIComInterface); // protect _unkBaseIComInterface // aggregate it with a helper Inner object _inner = new Inner(_unkBaseIComInterface); _unkInnerAggregated = Marshal.CreateAggregatedObject(_unkBaseIComInterface, _inner); // turn private OldLibrary.BaseClass.IComInterface into NewLibrary.IComInterface _baseIComInterface = (IComInterface)Marshal.GetTypedObjectForIUnknown(_unkInnerAggregated, typeof(NewLibrary.IComInterface)); Marshal.Release(_unkBaseIComInterface); }
public void Dispose() { // we may have recicular references to itself _baseIComInterface = null; if (_inner != null) { _inner.Dispose(); _inner = null; } if (_unkInnerAggregated != IntPtr.Zero) { Marshal.Release(_unkInnerAggregated); _unkInnerAggregated = IntPtr.Zero; } if (_unkBaseIComInterface != IntPtr.Zero) { Marshal.Release(_unkBaseIComInterface); _unkBaseIComInterface = IntPtr.Zero; } }
// constructor public ImprovedClass() { // aggregate the CCW object with the helper Inner object _baseClassComProxy = new BaseClassComProxy(this); _baseIComInterface = _baseClassComProxy.GetComInterface <IComInterface>(); }