diff --git a/src/CommandLine/Core/InstanceBuilder.cs b/src/CommandLine/Core/InstanceBuilder.cs index a1fdd9f2..5ea056d8 100644 --- a/src/CommandLine/Core/InstanceBuilder.cs +++ b/src/CommandLine/Core/InstanceBuilder.cs @@ -167,7 +167,7 @@ private static T BuildImmutable(Type typeInfo, Maybe> factory, IEnume if(ctor == null) { - throw new InvalidOperationException($"Type appears to be immutable, but no constructor found for type {typeInfo.FullName} to accept values."); + throw new InvalidOperationException($"Type {typeInfo.FullName} appears to be immutable, but no constructor found to accept values."); } var values = diff --git a/src/CommandLine/Infrastructure/ReflectionHelper.cs b/src/CommandLine/Infrastructure/ReflectionHelper.cs index a7926cb7..47fe70ea 100644 --- a/src/CommandLine/Infrastructure/ReflectionHelper.cs +++ b/src/CommandLine/Infrastructure/ReflectionHelper.cs @@ -91,6 +91,11 @@ public static T CreateDefaultImmutableInstance(Type[] constructorTypes) public static object CreateDefaultImmutableInstance(Type type, Type[] constructorTypes) { var ctor = type.GetTypeInfo().GetConstructor(constructorTypes); + if (ctor == null) + { + throw new InvalidOperationException($"Type {type.FullName} appears to be immutable, but no constructor found to accept values."); + } + var values = (from prms in ctor.GetParameters() select prms.ParameterType.CreateDefaultForImmutable()).ToArray(); return ctor.Invoke(values); diff --git a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs index c6234089..bb88595e 100644 --- a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs +++ b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs @@ -1155,7 +1155,17 @@ public void OptionClass_IsImmutable_HasNoCtor() { Action act = () => InvokeBuild(new string[] { "Test" }, false, false); - act.Should().Throw(); + act.Should().Throw() + .Which.Message.Should().Be("Type CommandLine.Tests.Unit.Core.InstanceBuilderTests+ValueWithNoSetterOptions appears to be immutable, but no constructor found to accept values."); + } + + [Fact] + public void OptionClass_IsImmutable_HasNoCtor_HelpRequested() + { + Action act = () => InvokeBuild(new string[] { "--help" }); + + act.Should().Throw() + .Which.Message.Should().Be("Type CommandLine.Tests.Unit.Core.InstanceBuilderTests+ValueWithNoSetterOptions appears to be immutable, but no constructor found to accept values."); } private class ValueWithNoSetterOptions