One way is to use a ServiceLocator that points to a specific container. Since the locator is usually displayed as a singleton, you get your container for free from anywhere in the code.
For example, in Unity, it will be similar to
// configure the locator somewhere early UnityServiceLocator locator = new UnityServiceLocator( container ); ServiceLocator.SetLocatorProvider( () => locator ); ... // get the container anywhere var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
Discussing whether a locator is antipattern is beyond the scope. In my opinion, this is at least no more "antipattern" than the whole IoC.
source share