Skip to content

Commit

Permalink
Modified the constructor not found exception message
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Csajtai committed May 9, 2017
1 parent a0ec1ee commit d4ffcf3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
30 changes: 23 additions & 7 deletions src/stashbox/Exceptions/ConstructorNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Stashbox.Exceptions
{
Expand All @@ -14,11 +12,29 @@ public class ConstructorNotFoundException : Exception
/// Constructs a <see cref="ConstructorNotFoundException"/>.
/// </summary>
/// <param name="type">The type on the constructor was not found.</param>
/// <param name="argumentsLength">The length of the arguments.</param>
/// <param name="argumentTypes">The arguments.</param>
/// <param name="innerException">The inner exception</param>
public ConstructorNotFoundException(Type type, int argumentsLength, Exception innerException = null) :
base($"Constructor not found for {type.FullName} with {argumentsLength} arguments.", innerException)
{
}
public ConstructorNotFoundException(Type type, Type[] argumentTypes, Exception innerException = null) :
base($"Constructor not found for {type.FullName} with the given argument types: {argumentTypes.Select(t => t.FullName).Aggregate((t1, t2) => $"{t1}, {t2}")}", innerException)
{ }

/// <summary>
/// Constructs a <see cref="ConstructorNotFoundException"/>.
/// </summary>
/// <param name="type">The type on the constructor was not found.</param>
/// <param name="innerException">The inner exception</param>
public ConstructorNotFoundException(Type type, Exception innerException = null) :
base($"Constructor not found for {type.FullName} with no arguments.", innerException)
{ }

/// <summary>
/// Constructs a <see cref="ConstructorNotFoundException"/>.
/// </summary>
/// <param name="type">The type on the constructor was not found.</param>
/// <param name="argument">The argument type.</param>
/// <param name="innerException">The inner exception</param>
public ConstructorNotFoundException(Type type, Type argument, Exception innerException = null) :
base($"Constructor not found for {type.FullName} with argument: {argument.FullName}", innerException)
{ }
}
}
18 changes: 15 additions & 3 deletions src/stashbox/Registration/RegistrationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,33 @@ public IFluentServiceRegistrator WithConstructorByArgumentTypes(params Type[] ar
{
var constructor = this.ImplementationType.GetConstructor(argumentTypes);
if (constructor == null)
throw new ConstructorNotFoundException(this.ImplementationType, argumentTypes.Length);
this.ThrowConstructorNotFoundException(this.ImplementationType, argumentTypes);

this.Context.SelectedConstructor = constructor;
return this;
}

public IFluentServiceRegistrator WithConstructorByArguments(params object[] arguments)
{
var constructor = this.ImplementationType.GetConstructor(arguments.Select(arg => arg.GetType()).ToArray());
var argTypes = arguments.Select(arg => arg.GetType()).ToArray();
var constructor = this.ImplementationType.GetConstructor(argTypes);
if (constructor == null)
throw new ConstructorNotFoundException(this.ImplementationType, arguments.Length);
this.ThrowConstructorNotFoundException(this.ImplementationType, argTypes);

this.Context.SelectedConstructor = constructor;
this.Context.ConstructorArguments = arguments;
return this;
}

private void ThrowConstructorNotFoundException(Type type, params Type[] argTypes)
{
if (argTypes.Length == 0)
throw new ConstructorNotFoundException(type);

if (argTypes.Length == 1)
throw new ConstructorNotFoundException(type, argTypes[0]);

throw new ConstructorNotFoundException(type, argTypes);
}
}
}

0 comments on commit d4ffcf3

Please sign in to comment.