-
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.
- Loading branch information
1 parent
d4523f0
commit 6c9913b
Showing
2 changed files
with
58 additions
and
0 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,57 @@ | ||
namespace RocketPluginHelper.Helpers; | ||
|
||
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)] | ||
[SuppressMessage("ReSharper", "ConvertIfStatementToSwitchStatement")] | ||
public static class DisposeHelper | ||
{ | ||
public static async Task DisposeAllAsync(this IEnumerable<object> objects) | ||
{ | ||
foreach (var @object in objects) | ||
{ | ||
if (@object is IAsyncDisposable asyncDisposable) | ||
{ | ||
await asyncDisposable.DisposeAsync(); | ||
} | ||
else if (@object is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} | ||
public static async Task DisposeAllAsync(this List<object> objects) | ||
{ | ||
for (var i = 0; i < objects.Count; i++) | ||
{ | ||
var @object = objects[i]; | ||
if (@object is IAsyncDisposable asyncDisposable) | ||
{ | ||
await asyncDisposable.DisposeAsync(); | ||
} | ||
else if (@object is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} | ||
public static void DisposeAll(this IEnumerable<object> objects) | ||
{ | ||
foreach (var @object in objects) | ||
{ | ||
if (@object is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} | ||
public static async Task DisposeSyncOrAsync(this object o) | ||
{ | ||
if (o is IAsyncDisposable asyncDisposable) | ||
{ | ||
await asyncDisposable.DisposeAsync(); | ||
} | ||
else if (o is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} |
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