diff --git a/NonSucking.Framework.Extension/IoC/StandaloneTypeContainer.cs b/NonSucking.Framework.Extension/IoC/StandaloneTypeContainer.cs index 5593082..1253fe7 100644 --- a/NonSucking.Framework.Extension/IoC/StandaloneTypeContainer.cs +++ b/NonSucking.Framework.Extension/IoC/StandaloneTypeContainer.cs @@ -128,11 +128,12 @@ public override T GetUnregistered() where T : class public override void Dispose() { typeRegister.Clear(); - typeInformationRegister.Values - .Where(t => t.Behaviour == InstanceBehaviour.Singleton && t.Instance != this) - .Select(t => t.Instance as IDisposable) - .ToList() - .ForEach(i => i?.Dispose()); + + typeInformationRegister.Remove(typeof(StandaloneTypeContainer)); + typeInformationRegister.Remove(typeof(ITypeContainer)); + + foreach (var item in typeInformationRegister.Values) + item.TryDispose(); typeInformationRegister.Clear(); typeInformationSemaphore.Dispose(); @@ -144,7 +145,7 @@ internal override void BuildCtorInformation(CtorInformation info) public override void Remove() => Remove(typeof(T)); - + public override void Remove(Type type) => typeInformationRegister.Remove(type); diff --git a/NonSucking.Framework.Extension/IoC/TypeInformation.cs b/NonSucking.Framework.Extension/IoC/TypeInformation.cs index b7ae095..77871b7 100644 --- a/NonSucking.Framework.Extension/IoC/TypeInformation.cs +++ b/NonSucking.Framework.Extension/IoC/TypeInformation.cs @@ -57,6 +57,19 @@ public void RecreateUncompleteCtors() Completed = !ctors.Any(c => !c.IsComplete); } + + /// + /// Disposed the singleton instance, if existing and required + /// + public void TryDispose() + { + if (Behaviour != InstanceBehaviour.Singleton + || singeltonInstance is not null) + return; + if (singeltonInstance is IDisposable disposable) + disposable.Dispose(); + + } } }