From 9309608b84405a0dbfbd51a6588a5f57dafd8b2b Mon Sep 17 00:00:00 2001 From: Sergey Komisarchik Date: Tue, 12 May 2020 00:19:25 +0300 Subject: [PATCH] show meaningfull message for a missing interface/abstract parameter type --- .../Configuration/StringArgumentValue.cs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs b/src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs index 0a7c09b..27834a1 100644 --- a/src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs +++ b/src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs @@ -93,20 +93,22 @@ public object ConvertTo(Type toType, ResolutionContext resolutionContext) // maybe it's the assembly-qualified type name of a concrete implementation // with a default constructor var type = FindType(argumentValue.Trim()); - if (type != null) + if (type == null) { - var ctor = type.GetTypeInfo().DeclaredConstructors.FirstOrDefault(ci => - { - var parameters = ci.GetParameters(); - return parameters.Length == 0 || parameters.All(pi => pi.HasDefaultValue); - }); + throw new InvalidOperationException($"Type {argumentValue} was not found."); + } - if (ctor == null) - throw new InvalidOperationException($"A default constructor was not found on {type.FullName}."); + var ctor = type.GetTypeInfo().DeclaredConstructors.FirstOrDefault(ci => + { + var parameters = ci.GetParameters(); + return parameters.Length == 0 || parameters.All(pi => pi.HasDefaultValue); + }); - var call = ctor.GetParameters().Select(pi => pi.DefaultValue).ToArray(); - return ctor.Invoke(call); - } + if (ctor == null) + throw new InvalidOperationException($"A default constructor was not found on {type.FullName}."); + + var call = ctor.GetParameters().Select(pi => pi.DefaultValue).ToArray(); + return ctor.Invoke(call); } return Convert.ChangeType(argumentValue, toType);