diff --git a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs index 86d9af68a8..d99dce3508 100644 --- a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs +++ b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs @@ -1836,4 +1836,44 @@ namespace Microsoft.ML.OnnxRuntime.Tests } } + + // A Disposable list is a list of IDisposable objects. All elements will be disposed when the container is disposed. + internal class DisposableList : List, IDisposableReadOnlyCollection + where T : IDisposable + { + public DisposableList() { } + public DisposableList(int count) : base(count) { } + + #region IDisposable Support + private bool disposedValue = false; // To detect redundant calls + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + for (int i = 0; i < this.Count; i++) + { + this[i]?.Dispose(); + } + this.Clear(); + } + + disposedValue = true; + } + } + + ~DisposableList() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + #endregion + } }