enable disposing ITypeResolver

This commit is contained in:
Thomas Freudenberg 2021-03-29 17:44:21 +02:00 committed by Patrik Svensson
parent 1ed7e65fcb
commit 6bceac8a5e
3 changed files with 19 additions and 3 deletions

View File

@ -4,7 +4,7 @@ using Spectre.Console.Cli;
namespace Injection namespace Injection
{ {
public sealed class TypeResolver : ITypeResolver public sealed class TypeResolver : ITypeResolver, IDisposable
{ {
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider;
@ -17,5 +17,13 @@ namespace Injection
{ {
return _provider.GetRequiredService(type); return _provider.GetRequiredService(type);
} }
public void Dispose()
{
if (_provider is IDisposable disposable)
{
disposable.Dispose();
}
}
} }
} }

View File

@ -76,7 +76,7 @@ namespace Spectre.Console.Cli
_registrar.RegisterInstance(typeof(IRemainingArguments), parsedResult.Remaining); _registrar.RegisterInstance(typeof(IRemainingArguments), parsedResult.Remaining);
// Create the resolver and the context. // Create the resolver and the context.
var resolver = new TypeResolverAdapter(_registrar.Build()); using var resolver = new TypeResolverAdapter(_registrar.Build());
var context = new CommandContext(parsedResult.Remaining, leaf.Command.Name, leaf.Command.Data); var context = new CommandContext(parsedResult.Remaining, leaf.Command.Name, leaf.Command.Data);
// Execute the command tree. // Execute the command tree.

View File

@ -2,7 +2,7 @@ using System;
namespace Spectre.Console.Cli namespace Spectre.Console.Cli
{ {
internal sealed class TypeResolverAdapter : ITypeResolver internal sealed class TypeResolverAdapter : ITypeResolver, IDisposable
{ {
private readonly ITypeResolver? _resolver; private readonly ITypeResolver? _resolver;
@ -43,5 +43,13 @@ namespace Spectre.Console.Cli
throw CommandRuntimeException.CouldNotResolveType(type, ex); throw CommandRuntimeException.CouldNotResolveType(type, ex);
} }
} }
public void Dispose()
{
if (_resolver is IDisposable disposable)
{
disposable.Dispose();
}
}
} }
} }