diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index 35590a7..87a260c 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -113,17 +113,6 @@ namespace LightweightIocContainer /// An instance of the given public T Resolve(params object[] arguments) => ResolveInternal(arguments); - /// - /// Gets an instance of the given - /// - /// The given - /// The constructor arguments - /// The current resolve stack - /// An instance of the given - /// Could not find function - internal object? Resolve(Type type, object?[]? arguments, List? resolveStack) => - GenericMethodCaller.CallPrivate(this, nameof(ResolveInternal), type, arguments, resolveStack); - /// /// Gets an instance of a given registered /// @@ -145,7 +134,7 @@ namespace LightweightIocContainer /// Tried resolving a multiton without scope argument /// No matching constructor for the given found /// Getting resolve stack failed without exception - private object TryResolve(object[]? arguments, List? resolveStack) + private object TryResolve(object?[]? arguments, List? resolveStack) { IRegistration registration = FindRegistration() ?? throw new TypeNotRegisteredException(typeof(T)); @@ -204,7 +193,7 @@ namespace LightweightIocContainer /// Tried resolving a multiton without scope argument /// No matching constructor for the given found /// Getting resolve stack failed without exception - private object? TryResolveNonGeneric(Type type, object[]? arguments, List resolveStack) => + internal object? TryResolveNonGeneric(Type type, object?[]? arguments, List? resolveStack) => GenericMethodCaller.CallPrivate(this, nameof(TryResolve), type, arguments, resolveStack); /// @@ -301,7 +290,7 @@ namespace LightweightIocContainer /// The given arguments /// The of the instance /// An already existing instance if possible, null if not - private object? TryGetExistingInstance(IRegistration registration, IReadOnlyList? arguments) => + private object? TryGetExistingInstance(IRegistration registration, IReadOnlyList? arguments) => registration switch { ITypedFactoryRegistration typedFactoryRegistration => typedFactoryRegistration.Factory.Factory, @@ -325,7 +314,7 @@ namespace LightweightIocContainer /// The given arguments /// A multiton instance if existing for the given , null if not /// Tried resolving a multiton without scope argument - private object? TryGetMultitonInstance(IMultitonRegistration registration, IReadOnlyList? arguments) + private object? TryGetMultitonInstance(IMultitonRegistration registration, IReadOnlyList? arguments) { if (arguments == null || !arguments.Any()) throw new MultitonResolveException("Can not resolve multiton without arguments.", registration.InterfaceType); @@ -394,7 +383,7 @@ namespace LightweightIocContainer /// The of the given /// The constructor arguments /// The argument list updated with the - private object[]? UpdateArgumentsWithRegistrationParameters(IWithParametersInternal registration, object[]? arguments) + private object?[]? UpdateArgumentsWithRegistrationParameters(IWithParametersInternal registration, object?[]? arguments) { if (registration.Parameters == null) return arguments; @@ -402,7 +391,7 @@ namespace LightweightIocContainer if (arguments != null && arguments.Any()) //if more arguments were passed to resolve { int argumentsSize = registration.Parameters.Length + arguments.Length; - object[] newArguments = new object[argumentsSize]; + object?[] newArguments = new object[argumentsSize]; for (int i = 0; i < argumentsSize; i++) { @@ -416,7 +405,7 @@ namespace LightweightIocContainer } } - object firstArgument = arguments.FirstOrGiven(a => a is not InternalResolvePlaceholder); //find the first argument that is not a placeholder + object? firstArgument = arguments.FirstOrGiven(a => a is not InternalResolvePlaceholder); //find the first argument that is not a placeholder if (firstArgument is InternalResolvePlaceholder) //no more arguments available break; //there won't be any more arguments @@ -445,7 +434,7 @@ namespace LightweightIocContainer /// parameters: The parameters needed to resolve the given /// exception: A if no matching constructor was found /// - private (bool result, List? parameters, NoMatchingConstructorFoundException? exception) TryGetTypeResolveStack(Type type, object[]? arguments, List resolveStack) + private (bool result, List? parameters, NoMatchingConstructorFoundException? exception) TryGetTypeResolveStack(Type type, object?[]? arguments, List resolveStack) { NoMatchingConstructorFoundException? noMatchingConstructorFoundException = null; @@ -477,7 +466,7 @@ namespace LightweightIocContainer /// parameters: The parameters needed to resolve the given /// exception: A List of s if the constructor is not matching /// - private (bool result, List? parameters, List? exceptions) TryGetConstructorResolveStack(ConstructorInfo constructor, object[]? arguments, List resolveStack) + private (bool result, List? parameters, List? exceptions) TryGetConstructorResolveStack(ConstructorInfo constructor, object?[]? arguments, List resolveStack) { List constructorParameters = constructor.GetParameters().ToList(); if (!constructorParameters.Any()) @@ -486,17 +475,17 @@ namespace LightweightIocContainer List exceptions = new(); List parameters = new(); - List? passedArguments = null; + List? passedArguments = null; if (arguments != null) - passedArguments = new List(arguments); + passedArguments = new List(arguments); foreach (ParameterInfo parameter in constructorParameters) { object? fittingArgument = new InternalResolvePlaceholder(); if (passedArguments != null) { - fittingArgument = passedArguments.FirstOrGiven(a => - a.GetType() == parameter.ParameterType || parameter.ParameterType.IsInstanceOfType(a)); + fittingArgument = passedArguments.FirstOrGiven(a => + a?.GetType() == parameter.ParameterType || parameter.ParameterType.IsInstanceOfType(a)); if (fittingArgument is not InternalResolvePlaceholder) passedArguments.Remove(fittingArgument); @@ -515,7 +504,7 @@ namespace LightweightIocContainer if (fittingArgument is InternalResolvePlaceholder && passedArguments != null) { - fittingArgument = passedArguments.FirstOrGiven(a => parameter.ParameterType.GetDefault() == a); + fittingArgument = passedArguments.FirstOrGiven(a => parameter.ParameterType.GetDefault() == a); if (fittingArgument is not InternalResolvePlaceholder) passedArguments.Remove(fittingArgument); diff --git a/LightweightIocContainer/LightweightIocContainer.xml b/LightweightIocContainer/LightweightIocContainer.xml index 67d8d75..92d9851 100644 --- a/LightweightIocContainer/LightweightIocContainer.xml +++ b/LightweightIocContainer/LightweightIocContainer.xml @@ -935,16 +935,6 @@ The constructor arguments An instance of the given - - - Gets an instance of the given - - The given - The constructor arguments - The current resolve stack - An instance of the given - Could not find function - Gets an instance of a given registered diff --git a/LightweightIocContainer/Validation/IocValidator.cs b/LightweightIocContainer/Validation/IocValidator.cs index 275c878..231e801 100644 --- a/LightweightIocContainer/Validation/IocValidator.cs +++ b/LightweightIocContainer/Validation/IocValidator.cs @@ -72,7 +72,7 @@ namespace LightweightIocContainer.Validation { try { - _iocContainer.Resolve(type, arguments, null); + _iocContainer.TryResolveNonGeneric(type, arguments, null); } catch (Exception exception) { @@ -81,7 +81,22 @@ namespace LightweightIocContainer.Validation } private T GetMock() where T : class => new Mock().Object; - private object? GetMockOrDefault(Type type) => - type.IsValueType ? Activator.CreateInstance(type) : GenericMethodCaller.CallPrivate(this, nameof(GetMock), type); + private object? GetMockOrDefault(Type type) + { + if (type.IsValueType) + return Activator.CreateInstance(type); + + if (type == typeof(string)) + return string.Empty; + + try + { + return GenericMethodCaller.CallPrivate(this, nameof(GetMock), type); + } + catch (Exception) + { + return null; + } + } } } \ No newline at end of file diff --git a/Test.LightweightIocContainer/IocValidatorTest.cs b/Test.LightweightIocContainer/IocValidatorTest.cs index 818e8bd..a9037d3 100644 --- a/Test.LightweightIocContainer/IocValidatorTest.cs +++ b/Test.LightweightIocContainer/IocValidatorTest.cs @@ -81,7 +81,7 @@ namespace Test.LightweightIocContainer validator.Validate(); - parameterMock.Verify(p => p.Method(), Times.Once); + parameterMock.Verify(p => p.Method(), Times.Never); } [Test]