Removes automatic registration of settings

Original intention was to register Settings automatically for DI. This ran into problem when the container verifies the configuration is valid for a settings using the constructor for initialization. It tries to resolve the parameters and fails.

This removes the automatic registration and falls back ActivatorCreateInstance when no Setting is registered.
This commit is contained in:
Phil Scott
2021-04-14 21:20:38 -04:00
committed by Patrik Svensson
parent 23564612c1
commit f5a9c0ca26
3 changed files with 20 additions and 11 deletions

View File

@ -25,9 +25,21 @@ namespace Spectre.Console.Cli
private static CommandSettings CreateSettings(ITypeResolver resolver, Type settingsType)
{
if (resolver.Resolve(settingsType) is CommandSettings settings)
try
{
return settings;
if (resolver.Resolve(settingsType) is CommandSettings settings)
{
return settings;
}
}
catch
{
// ignored
}
if (Activator.CreateInstance(settingsType) is CommandSettings instance)
{
return instance;
}
throw CommandParseException.CouldNotCreateSettings(settingsType);

View File

@ -31,11 +31,6 @@ namespace Spectre.Console.Cli
registrar?.Register(command.CommandType, command.CommandType);
}
if (!command.SettingsType.IsAbstract)
{
registrar?.Register(command.SettingsType, command.SettingsType);
}
foreach (var parameter in command.Parameters)
{
var pairDeconstructor = parameter?.PairDeconstructor?.Type;