Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NullReferenceException when creating a default immutable instance #495

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CommandLine/Core/InstanceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private static T BuildImmutable<T>(Type typeInfo, Maybe<Func<T>> 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 =
Expand Down
5 changes: 5 additions & 0 deletions src/CommandLine/Infrastructure/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public static T CreateDefaultImmutableInstance<T>(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);
Expand Down
12 changes: 11 additions & 1 deletion tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,17 @@ public void OptionClass_IsImmutable_HasNoCtor()
{
Action act = () => InvokeBuild<ValueWithNoSetterOptions>(new string[] { "Test" }, false, false);

act.Should().Throw<InvalidOperationException>();
act.Should().Throw<InvalidOperationException>()
.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<ValueWithNoSetterOptions>(new string[] { "--help" });

act.Should().Throw<InvalidOperationException>()
.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
Expand Down