-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from rioil/fix-issue22
COM参照を使用しない実装に変更
- Loading branch information
Showing
3 changed files
with
138 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace VRChatLogWathcer.Utils | ||
{ | ||
/// <summary> | ||
/// COMオブジェクトの開放処理を確実に行うためのラッパーオブジェクト | ||
/// </summary> | ||
internal class COMObject : IDisposable | ||
{ | ||
private dynamic? _object; | ||
private bool _isDisposed; | ||
|
||
public dynamic Object => (_isDisposed || _object is null) ? throw new ObjectDisposedException(nameof(COMObject)) : _object; | ||
|
||
public COMObject(dynamic comObject) | ||
{ | ||
_object = comObject; | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!_isDisposed) | ||
{ | ||
//if (disposing) | ||
//{ | ||
// // TODO: マネージド状態を破棄します (マネージド オブジェクト) | ||
//} | ||
|
||
Marshal.ReleaseComObject(_object); | ||
_object = null; | ||
_isDisposed = true; | ||
} | ||
} | ||
|
||
~COMObject() | ||
{ | ||
// このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | ||
Dispose(disposing: false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
// このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// COMオブジェクトの開放処理を確実に行うためのラッパーオブジェクト | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
internal class COMObject<T> : IDisposable | ||
{ | ||
private dynamic? _object; | ||
private bool _isDisposed; | ||
|
||
public dynamic Object => (_isDisposed || _object is null) ? throw new ObjectDisposedException(nameof(COMObject)) : _object; | ||
|
||
public T Casted => (T)Object; | ||
|
||
public COMObject(dynamic comObject) | ||
{ | ||
_object = comObject; | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!_isDisposed) | ||
{ | ||
//if (disposing) | ||
//{ | ||
// // TODO: マネージド状態を破棄します (マネージド オブジェクト) | ||
//} | ||
|
||
Marshal.ReleaseComObject(_object); | ||
_object = null; | ||
_isDisposed = true; | ||
} | ||
} | ||
|
||
~COMObject() | ||
{ | ||
// このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | ||
Dispose(disposing: false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
// このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
public static implicit operator T(COMObject<T> obj) => obj.Casted; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters