mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 08:52:50 +08:00
(#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:
parent
21ac952307
commit
2f6b4f53c4
@ -15,7 +15,12 @@ namespace Spectre.Console.Examples
|
||||
|
||||
public object Resolve(Type type)
|
||||
{
|
||||
return _provider.GetRequiredService(type);
|
||||
if (type == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return _provider.GetService(type);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
@ -11,7 +11,7 @@ namespace Spectre.Console.Cli
|
||||
/// Resolves an instance of the specified type.
|
||||
/// </summary>
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
|
@ -27,18 +27,11 @@ namespace Spectre.Console.Cli
|
||||
}
|
||||
|
||||
private static CommandSettings CreateSettings(ITypeResolver resolver, Type settingsType)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (resolver.Resolve(settingsType) is CommandSettings settings)
|
||||
{
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
|
||||
if (Activator.CreateInstance(settingsType) is CommandSettings instance)
|
||||
{
|
||||
|
@ -20,14 +20,9 @@ namespace Spectre.Console.Cli
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user