mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
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:

committed by
Patrik Svensson

parent
23564612c1
commit
f5a9c0ca26
@ -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);
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user