diff --git a/LightweightIocContainer/EnumerableExtension.cs b/LightweightIocContainer/EnumerableExtension.cs
new file mode 100644
index 0000000..99f150c
--- /dev/null
+++ b/LightweightIocContainer/EnumerableExtension.cs
@@ -0,0 +1,60 @@
+// // Author: Gockner, Simon
+// // Created: 2019-07-02
+// // Copyright(c) 2019 SimonG. All Rights Reserved.
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace LightweightIocContainer
+{
+ internal static class EnumerableExtension
+ {
+ ///
+ /// Returns the first element of a , or a new instance of a given if the contains no elements
+ ///
+ /// The source of the
+ /// The given to return if the contains no elements
+ /// The given
+ /// The first element of the , or a new instance of a given if the contains no elements
+ public static TSource FirstOrGiven(this IEnumerable source) where TGiven : TSource, new() =>
+ source.TryGetFirst(null);
+
+ ///
+ /// Returns the first element of a that satisfies a condition, or a new instance of a given if no such element is found
+ ///
+ /// The source of the
+ /// The given to return if the contains no element that satisfies the given condition
+ /// The given
+ /// A function to test each element for a condition
+ /// The first element of the that satisfies a condition, or a new instance of the given if no such element is found
+ public static TSource FirstOrGiven(this IEnumerable source, Func predicate) where TGiven : TSource, new() =>
+ source.TryGetFirst(predicate);
+
+ ///
+ /// Tries to get the first element of the given or creates a new element of a given when no element is found
+ ///
+ /// The source of the
+ /// The given to create a new element when no fitting element is found
+ /// The given
+ /// A function to test each element for a condition
+ /// The first element of the or a new instance of the given when no element is found
+ private static TSource TryGetFirst(this IEnumerable source, Func predicate) where TGiven : TSource, new()
+ {
+ try
+ {
+ TSource first;
+ if (predicate == null)
+ first = source.First();
+ else
+ first = source.First(predicate);
+
+ return first;
+ }
+ catch (Exception)
+ {
+ return new TGiven();
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs
index 7009119..256d929 100644
--- a/LightweightIocContainer/IocContainer.cs
+++ b/LightweightIocContainer/IocContainer.cs
@@ -80,7 +80,7 @@ namespace LightweightIocContainer
/// The constructor arguments
/// An instance of the given type
/// Could not find function
- public object Resolve(Type type, object[] arguments) //somehow the order of the arguments is different in the application compared to the unit test
+ public object Resolve(Type type, object[] arguments)
{
MethodInfo resolveMethod = typeof(IocContainer).GetMethod(nameof(ResolveInternal), BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo genericResolveMethod = resolveMethod?.MakeGenericMethod(type);
@@ -218,18 +218,37 @@ namespace LightweightIocContainer
ParameterInfo[] parameters = ctor.GetParameters();
foreach (var parameter in parameters)
{
- object fittingArgument = null;
+ object fittingArgument = new InternalResolvePlaceholder();
if (argumentsList != null)
{
- fittingArgument = argumentsList.FirstOrDefault(a => a?.GetType() == parameter.ParameterType);
- if (fittingArgument != null)
+ fittingArgument = argumentsList.FirstOrGiven