Browse Source

Added general scope guard utility

pull/34/head
Anairkoen Schno 4 years ago
parent
commit
b199418898
1 changed files with 34 additions and 0 deletions
  1. +34
    -0
      IPA.Loader/Utilities/Utils.cs

+ 34
- 0
IPA.Loader/Utilities/Utils.cs View File

@ -190,6 +190,40 @@ namespace IPA.Utilities
return cmpVal;
}
/// <summary>
/// An object used to manage scope guards.
/// </summary>
/// <example>
/// <code>
/// using var _ = new Utils.ScopeGuardObject(() => RunOnScopeExit(value));
/// </code>
/// </example>
/// <seealso cref="ScopeGuard(Action)"/>
public struct ScopeGuardObject : IDisposable
{
private readonly Action action;
/// <summary>
/// Creates a new scope guard that will invoke <paramref name="action"/> when disposed.
/// </summary>
/// <param name="action">the action to run on dispose</param>
public ScopeGuardObject(Action action)
=> this.action = action;
void IDisposable.Dispose()
=> action?.Invoke();
}
/// <summary>
/// Creates a scope guard for a given <see cref="Action"/>.
/// </summary>
/// <param name="action">teh <see cref="Action"/> to run on dispose</param>
/// <returns>a <see cref="ScopeGuardObject"/> that will run <paramref name="action"/> on disposal</returns>
/// <example>
/// <code>
/// using var _ = Utils.ScopeGuard(() => RunOnScopeExit(value));
/// </code>
/// </example>
public static ScopeGuardObject ScopeGuard(Action action)
=> new ScopeGuardObject(action);
internal static bool HasInterface(this TypeDefinition type, string interfaceFullName)
{


Loading…
Cancel
Save