(#555) Clarify ITypeResolver returns null

and does not throw on unresolvable types.
Also changed the TypeResolverAdapter to adhere
to those expectations and removed the now no longer
needed try-catch from CommandPropertyBinder.
This commit is contained in:
Nils Andresen 2021-11-13 21:27:10 +01:00 committed by Patrik Svensson
parent 21ac952307
commit 2f6b4f53c4
4 changed files with 11 additions and 18 deletions

View File

@ -15,7 +15,12 @@ namespace Spectre.Console.Examples
public object Resolve(Type type) public object Resolve(Type type)
{ {
return _provider.GetRequiredService(type); if (type == null)
{
return null;
}
return _provider.GetService(type);
} }
public void Dispose() public void Dispose()

View File

@ -11,7 +11,7 @@ namespace Spectre.Console.Cli
/// Resolves an instance of the specified type. /// Resolves an instance of the specified type.
/// </summary> /// </summary>
/// <param name="type">The type to resolve.</param> /// <param name="type">The type to resolve.</param>
/// <returns>An instance of the specified type.</returns> /// <returns>An instance of the specified type, or <c>null</c> if no registration for the specified type exists.</returns>
object? Resolve(Type? type); object? Resolve(Type? type);
} }
} }

View File

@ -27,18 +27,11 @@ namespace Spectre.Console.Cli
} }
private static CommandSettings CreateSettings(ITypeResolver resolver, Type settingsType) private static CommandSettings CreateSettings(ITypeResolver resolver, Type settingsType)
{
try
{ {
if (resolver.Resolve(settingsType) is CommandSettings settings) if (resolver.Resolve(settingsType) is CommandSettings settings)
{ {
return settings; return settings;
} }
}
catch
{
// ignored
}
if (Activator.CreateInstance(settingsType) is CommandSettings instance) if (Activator.CreateInstance(settingsType) is CommandSettings instance)
{ {

View File

@ -20,14 +20,9 @@ namespace Spectre.Console.Cli
try try
{ {
if (_resolver != null) var obj = _resolver?.Resolve(type);
if (obj != null)
{ {
var obj = _resolver.Resolve(type);
if (obj == null)
{
throw CommandRuntimeException.CouldNotResolveType(type);
}
return obj; return obj;
} }