Catel.IoC.ServiceLocator.Dispose C# (CSharp) Method

Dispose() public method

Disposes this instance and all registered instances.
public Dispose ( ) : void
return void
        public void Dispose()
        {
            lock (_lockObject)
            {
                foreach (var registeredInstance in _registeredInstances)
                {
                    var instance = registeredInstance.Value.ImplementingInstance;
                    if (ReferenceEquals(this, instance))
                    {
                        continue;
                    }

                    var disposable = instance as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }

                _registeredInstances.Clear();
                _registeredTypes.Clear();
            }
        }
        #endregion

Usage Example

Ejemplo n.º 1
0
            public void DisposesAllDisposableInstances()
            {
                var isDisposed = false;

                var disposable = new Disposable();
                disposable.Disposed += (sender, e) => isDisposed = true;

                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterInstance(typeof(Disposable), disposable);

                serviceLocator.Dispose();

                Assert.IsTrue(isDisposed);
            }