I created a client for a communicationserver to facilitate integration of telephony functions with an intranet webapplication. This client can be instantiated in a regular managed application (eg WPF, WinForms) but it can also be used by an ActiveX host such as Internet Explorer.
The actual interface that te client implements does not provide a means to dispose of resources. I cannot change this interface b/c that would break existing code (in particular: an existing webapplication using another client through COM/ActiveX with the same interface).
The Dispose pattern is implemented for use in managed applications. A finalizer is created to free unmanaged resources during finalization.
When the client is used through COM/ActiveX, Dispose() is never called (b/c the COM Visible interface doesn't support it). The finalizer is called. Not deterministically but eventually it is called. The client wraps some stuff (timers eg) that need to be disposed of. I want to break the Dispose pattern by disposing of managed resources in the finalizer of the wrapping class assuming that the finalizer is the only place to do so. I'd use GC.KeepAlive to make sure that important managed instances will stay alive to prevent problems while disposing.
So I need a way to tell at runtime whether I'm confortably inside a managed environment that will use the Dispose pattern as it is intended or if I can savely break that pattern b/c I'm running inside an ActiveX host.