Skip to content

Commit

Permalink
Merge pull request #220 from skomis-mm/missingTypeMsg
Browse files Browse the repository at this point in the history
Show better error message for a missing interface parameter type
  • Loading branch information
Sergey Komisarchik authored May 11, 2020
2 parents cc45629 + 9309608 commit 8bea591
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8bea591

Please sign in to comment.