microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/email/COMObject.cs
61lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Runtime.InteropServices; |
| 5 | |
| 6 | namespace TypeAgent; |
| 7 | |
| 8 | public class COMObject : IDisposable |
| 9 | { |
| 10 | bool _disposed; |
| 11 | |
| 12 | public COMObject() |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | ~COMObject() |
| 17 | { |
| 18 | Dispose(false); |
| 19 | } |
| 20 | |
| 21 | public void Dispose() |
| 22 | { |
| 23 | Dispose(true); |
| 24 | GC.SuppressFinalize(this); |
| 25 | } |
| 26 | |
| 27 | #pragma warning disable CA1063 |
| 28 | void Dispose(bool fromDispose) |
| 29 | { |
| 30 | if (!_disposed) |
| 31 | { |
| 32 | OnDispose(); |
| 33 | _disposed = true; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | protected virtual void OnDispose() {} |
| 38 | |
| 39 | public static void Release(object value) |
| 40 | { |
| 41 | #pragma warning disable CA1416 |
| 42 | if (value != null) |
| 43 | { |
| 44 | Marshal.ReleaseComObject(value); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public static void Release(IEnumerable<object> values) |
| 49 | { |
| 50 | foreach (object value in values) |
| 51 | { |
| 52 | Release(value); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public static void ReleaseAll() |
| 57 | { |
| 58 | GC.Collect(); |
| 59 | GC.WaitForFullGCComplete(); |
| 60 | } |
| 61 | } |