#54: enabled nullable

- fix nullable warnings
pull/57/head
Simon G 4 years ago
parent b4cf096e96
commit b7d0db3c9b
  1. 2
      LightweightIocContainer/ActionExtension.cs
  2. 2
      LightweightIocContainer/EnumerableExtension.cs
  3. 11
      LightweightIocContainer/Exceptions/IocContainerException.cs
  4. 6
      LightweightIocContainer/Exceptions/NoMatchingConstructorFoundException.cs
  5. 12
      LightweightIocContainer/Factories/TypedFactory.cs
  6. 8
      LightweightIocContainer/GenericMethodCaller.cs
  7. 4
      LightweightIocContainer/Interfaces/Registrations/Fluent/IOnCreate.cs
  8. 2
      LightweightIocContainer/Interfaces/Registrations/Fluent/IWithFactory.cs
  9. 2
      LightweightIocContainer/Interfaces/Registrations/Fluent/IWithParameters.cs
  10. 2
      LightweightIocContainer/Interfaces/Registrations/ISingleTypeRegistration.cs
  11. 65
      LightweightIocContainer/IocContainer.cs
  12. 1
      LightweightIocContainer/LightweightIocContainer.csproj
  13. 112
      LightweightIocContainer/LightweightIocContainer.xml
  14. 2
      LightweightIocContainer/Registrations/MultipleMultitonRegistration.cs
  15. 14
      LightweightIocContainer/Registrations/MultipleRegistration.cs
  16. 4
      LightweightIocContainer/Registrations/RegistrationBase.cs
  17. 2
      LightweightIocContainer/Registrations/SingleTypeRegistration.cs
  18. 8
      LightweightIocContainer/Registrations/TypedRegistration.cs
  19. 4
      LightweightIocContainer/ResolvePlaceholders/InternalToBeResolvedPlaceholder.cs
  20. 2
      LightweightIocContainer/TypeExtension.cs
  21. 6
      LightweightIocContainer/Validation/IocValidator.cs

@ -15,7 +15,7 @@ namespace LightweightIocContainer
/// <typeparam name="T2">The <see cref="Type"/> of the given <see cref="Action{T2}"/>, has to implement <typeparamref name="T1"/></typeparam> /// <typeparam name="T2">The <see cref="Type"/> of the given <see cref="Action{T2}"/>, has to implement <typeparamref name="T1"/></typeparam>
/// <param name="action">The given <see cref="Action{T2}"/> to convert</param> /// <param name="action">The given <see cref="Action{T2}"/> to convert</param>
/// <returns>An <see cref="Action{T1}"/> converted from the given <see cref="Action{T2}"/></returns> /// <returns>An <see cref="Action{T1}"/> converted from the given <see cref="Action{T2}"/></returns>
public static Action<T1> Convert<T1, T2>(this Action<T2> action) where T1 : T2 public static Action<T1>? Convert<T1, T2>(this Action<T2>? action) where T1 : T2
{ {
if (action == null) if (action == null)
return null; return null;

@ -39,7 +39,7 @@ namespace LightweightIocContainer
/// <param name="source">The given <see cref="IEnumerable{T}"/></param> /// <param name="source">The given <see cref="IEnumerable{T}"/></param>
/// <param name="predicate">A function to test each element for a condition</param> /// <param name="predicate">A function to test each element for a condition</param>
/// <returns>The first element of the <see cref="IEnumerable{T}"/> or a new instance of the given <see cref="Type"/> when no element is found</returns> /// <returns>The first element of the <see cref="IEnumerable{T}"/> or a new instance of the given <see cref="Type"/> when no element is found</returns>
private static TSource TryGetFirst<TSource, TGiven>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) where TGiven : TSource, new() private static TSource TryGetFirst<TSource, TGiven>(this IEnumerable<TSource> source, Func<TSource, bool>? predicate) where TGiven : TSource, new()
{ {
try try
{ {

@ -16,20 +16,15 @@ namespace LightweightIocContainer.Exceptions
/// <summary> /// <summary>
/// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/> /// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/>
/// </summary> /// </summary>
protected IocContainerException() protected IocContainerException() => InnerExceptions = new List<Exception>();
{
}
/// <summary> /// <summary>
/// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/> /// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/>
/// </summary> /// </summary>
/// <param name="message">The message of the <see cref="Exception"/></param> /// <param name="message">The message of the <see cref="Exception"/></param>
protected IocContainerException(string message) protected IocContainerException(string message)
: base(message) : base(message) =>
{ InnerExceptions = new List<Exception>();
}
/// <summary> /// <summary>
/// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/> /// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/>

@ -4,7 +4,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace LightweightIocContainer.Exceptions namespace LightweightIocContainer.Exceptions
{ {
@ -17,12 +16,11 @@ namespace LightweightIocContainer.Exceptions
/// No matching constructor was found for the given or resolvable arguments /// No matching constructor was found for the given or resolvable arguments
/// </summary> /// </summary>
/// <param name="type">The <see cref="Type"/> with no matching constructor</param> /// <param name="type">The <see cref="Type"/> with no matching constructor</param>
/// <param name="exceptions">The inner exceptions of type <see cref="ConstructorNotMatchingException"/></param> public NoMatchingConstructorFoundException(Type type)
public NoMatchingConstructorFoundException(Type type, params ConstructorNotMatchingException[] exceptions)
: base($"No matching constructor for {type} found.") : base($"No matching constructor for {type} found.")
{ {
Type = type; Type = type;
InnerExceptions = exceptions == null ? new List<Exception>() : exceptions.OfType<Exception>().ToList(); InnerExceptions = new List<Exception>();
} }

@ -24,7 +24,7 @@ namespace LightweightIocContainer.Factories
/// The /// The
/// </summary> /// </summary>
/// <param name="container">The current instance of the <see cref="IIocContainer"/></param> /// <param name="container">The current instance of the <see cref="IIocContainer"/></param>
public TypedFactory(IIocContainer container) => CreateFactory(container); public TypedFactory(IIocContainer container) => Factory = CreateFactory(container);
/// <summary> /// <summary>
/// The implemented abstract typed factory/> /// The implemented abstract typed factory/>
@ -36,7 +36,7 @@ namespace LightweightIocContainer.Factories
/// </summary> /// </summary>
/// <exception cref="InvalidFactoryRegistrationException">Factory registration is invalid</exception> /// <exception cref="InvalidFactoryRegistrationException">Factory registration is invalid</exception>
/// <exception cref="IllegalAbstractMethodCreationException">Creation of abstract methods are illegal in their current state</exception> /// <exception cref="IllegalAbstractMethodCreationException">Creation of abstract methods are illegal in their current state</exception>
private void CreateFactory(IIocContainer container) private TFactory CreateFactory(IIocContainer container)
{ {
Type factoryType = typeof(TFactory); Type factoryType = typeof(TFactory);
@ -92,7 +92,7 @@ namespace LightweightIocContainer.Factories
} }
else else
{ {
MethodInfo emptyArray = typeof(Array).GetMethod(nameof(Array.Empty))?.MakeGenericMethod(typeof(object)); MethodInfo? emptyArray = typeof(Array).GetMethod(nameof(Array.Empty))?.MakeGenericMethod(typeof(object));
generator.EmitCall(OpCodes.Call, emptyArray, null); generator.EmitCall(OpCodes.Call, emptyArray, null);
} }
@ -102,7 +102,7 @@ namespace LightweightIocContainer.Factories
} }
//if factory contains a method to clear multiton instances //if factory contains a method to clear multiton instances
MethodInfo multitonClearMethod = factoryType.GetMethods().FirstOrDefault(m => m.Name.Equals(CLEAR_MULTITON_INSTANCE_METHOD_NAME)); MethodInfo? multitonClearMethod = factoryType.GetMethods().FirstOrDefault(m => m.Name.Equals(CLEAR_MULTITON_INSTANCE_METHOD_NAME));
if (multitonClearMethod != null) if (multitonClearMethod != null)
{ {
//create a method that looks like this //create a method that looks like this
@ -113,7 +113,7 @@ namespace LightweightIocContainer.Factories
if (multitonClearMethod.IsGenericMethod) if (multitonClearMethod.IsGenericMethod)
{ {
Type typeToClear = multitonClearMethod.GetGenericArguments().FirstOrDefault(); Type? typeToClear = multitonClearMethod.GetGenericArguments().FirstOrDefault();
if (typeToClear == null) if (typeToClear == null)
throw new IllegalAbstractMethodCreationException("No Type to clear specified.", multitonClearMethod); throw new IllegalAbstractMethodCreationException("No Type to clear specified.", multitonClearMethod);
@ -136,7 +136,7 @@ namespace LightweightIocContainer.Factories
} }
} }
Factory = (TFactory) Activator.CreateInstance(typeBuilder.CreateTypeInfo().AsType(), container); return (TFactory) Activator.CreateInstance(typeBuilder.CreateTypeInfo()?.AsType(), container);
} }
} }
} }

@ -24,10 +24,10 @@ namespace LightweightIocContainer
/// <returns>The result of invoking the method</returns> /// <returns>The result of invoking the method</returns>
/// <exception cref="GenericMethodNotFoundException">Could not find the generic method</exception> /// <exception cref="GenericMethodNotFoundException">Could not find the generic method</exception>
/// <exception cref="Exception">Any <see cref="Exception"/> thrown after invoking the generic method</exception> /// <exception cref="Exception">Any <see cref="Exception"/> thrown after invoking the generic method</exception>
public static object Call(object caller, string functionName, Type genericParameter, BindingFlags bindingFlags, params object[] parameters) public static object Call(object caller, string functionName, Type genericParameter, BindingFlags bindingFlags, params object?[] parameters)
{ {
MethodInfo method = caller.GetType().GetMethod(functionName, bindingFlags); MethodInfo? method = caller.GetType().GetMethod(functionName, bindingFlags);
MethodInfo genericMethod = method?.MakeGenericMethod(genericParameter); MethodInfo? genericMethod = method?.MakeGenericMethod(genericParameter);
if (genericMethod == null) if (genericMethod == null)
throw new GenericMethodNotFoundException(functionName); throw new GenericMethodNotFoundException(functionName);
@ -52,7 +52,7 @@ namespace LightweightIocContainer
/// <returns>The result of invoking the method</returns> /// <returns>The result of invoking the method</returns>
/// <exception cref="GenericMethodNotFoundException">Could not find the generic method</exception> /// <exception cref="GenericMethodNotFoundException">Could not find the generic method</exception>
/// <exception cref="Exception">Any <see cref="Exception"/> thrown after invoking the generic method</exception> /// <exception cref="Exception">Any <see cref="Exception"/> thrown after invoking the generic method</exception>
public static object CallPrivate(object caller, string functionName, Type genericParameter, params object[] parameters) => public static object CallPrivate(object caller, string functionName, Type genericParameter, params object?[] parameters) =>
Call(caller, functionName, genericParameter, BindingFlags.NonPublic | BindingFlags.Instance, parameters); Call(caller, functionName, genericParameter, BindingFlags.NonPublic | BindingFlags.Instance, parameters);
} }
} }

@ -16,7 +16,7 @@ namespace LightweightIocContainer.Interfaces.Registrations.Fluent
/// This <see cref="Action"/> is invoked when an instance of this type is created. /// This <see cref="Action"/> is invoked when an instance of this type is created.
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IOnCreate{TInterface, TImplementation}.OnCreate"/></para> /// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IOnCreate{TInterface, TImplementation}.OnCreate"/></para>
/// </summary> /// </summary>
internal Action<object> OnCreateAction { get; } internal Action<object?>? OnCreateAction { get; }
} }
/// <summary> /// <summary>
@ -31,6 +31,6 @@ namespace LightweightIocContainer.Interfaces.Registrations.Fluent
/// </summary> /// </summary>
/// <param name="action">The <see cref="Action{T}"/></param> /// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
ITypedRegistration<TInterface, TImplementation> OnCreate(Action<TImplementation> action); ITypedRegistration<TInterface, TImplementation> OnCreate(Action<TImplementation?> action);
} }
} }

@ -32,6 +32,6 @@ namespace LightweightIocContainer.Interfaces.Registrations.Fluent
/// <summary> /// <summary>
/// The Factory added with the <see cref="IWithFactory.WithFactory{TFactory}"/> method /// The Factory added with the <see cref="IWithFactory.WithFactory{TFactory}"/> method
/// </summary> /// </summary>
ITypedFactory Factory { get; } ITypedFactory? Factory { get; }
} }
} }

@ -38,6 +38,6 @@ namespace LightweightIocContainer.Interfaces.Registrations.Fluent
/// An <see cref="Array"/> of parameters that are used to <see cref="IocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/> /// An <see cref="Array"/> of parameters that are used to <see cref="IocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/>
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IWithParameters.WithParameters(object[])"/></para> /// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IWithParameters.WithParameters(object[])"/></para>
/// </summary> /// </summary>
object[] Parameters { get; } object[]? Parameters { get; }
} }
} }

@ -15,7 +15,7 @@ namespace LightweightIocContainer.Interfaces.Registrations
/// <summary> /// <summary>
/// <see cref="Func{T,TResult}"/> that is invoked instead of creating an instance of this <see cref="Type"/> the default way /// <see cref="Func{T,TResult}"/> that is invoked instead of creating an instance of this <see cref="Type"/> the default way
/// </summary> /// </summary>
Func<IResolver, T> FactoryMethod { get; } Func<IResolver, T>? FactoryMethod { get; }
/// <summary> /// <summary>
/// Pass a <see cref="Func{T,TResult}"/> that will be invoked instead of creating an instance of this <see cref="Type"/> the default way /// Pass a <see cref="Func{T,TResult}"/> that will be invoked instead of creating an instance of this <see cref="Type"/> the default way

@ -7,7 +7,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using LightweightIocContainer.Exceptions; using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Factories; using LightweightIocContainer.Interfaces.Factories;
@ -26,8 +25,8 @@ namespace LightweightIocContainer
{ {
private readonly RegistrationFactory _registrationFactory; private readonly RegistrationFactory _registrationFactory;
private readonly List<(Type type, object instance)> _singletons = new(); private readonly List<(Type type, object? instance)> _singletons = new();
private readonly List<(Type type, Type scope, ConditionalWeakTable<object, object> instances)> _multitons = new(); private readonly List<(Type type, Type scope, ConditionalWeakTable<object, object?> instances)> _multitons = new();
/// <summary> /// <summary>
/// The main container that carries all the <see cref="IRegistration"/>s and can resolve all the types you'll ever want /// The main container that carries all the <see cref="IRegistration"/>s and can resolve all the types you'll ever want
@ -266,7 +265,7 @@ namespace LightweightIocContainer
/// <param name="resolveStack">The current resolve stack</param> /// <param name="resolveStack">The current resolve stack</param>
/// <returns>An instance of the given <see cref="Type"/></returns> /// <returns>An instance of the given <see cref="Type"/></returns>
/// <exception cref="InternalResolveException">Could not find function <see cref="ResolveInternal{T}"/></exception> /// <exception cref="InternalResolveException">Could not find function <see cref="ResolveInternal{T}"/></exception>
internal object Resolve(Type type, object[] arguments, List<Type> resolveStack) => internal object Resolve(Type type, object?[]? arguments, List<Type>? resolveStack) =>
GenericMethodCaller.CallPrivate(this, nameof(ResolveInternal), type, arguments, resolveStack); GenericMethodCaller.CallPrivate(this, nameof(ResolveInternal), type, arguments, resolveStack);
/// <summary> /// <summary>
@ -301,7 +300,7 @@ namespace LightweightIocContainer
/// <returns>An instance of the given registered <see cref="Type"/></returns> /// <returns>An instance of the given registered <see cref="Type"/></returns>
/// <exception cref="TypeNotRegisteredException">The given <see cref="Type"/> is not registered in this <see cref="IocContainer"/></exception> /// <exception cref="TypeNotRegisteredException">The given <see cref="Type"/> is not registered in this <see cref="IocContainer"/></exception>
/// <exception cref="UnknownRegistrationException">The registration for the given <see cref="Type"/> has an unknown <see cref="Type"/></exception> /// <exception cref="UnknownRegistrationException">The registration for the given <see cref="Type"/> has an unknown <see cref="Type"/></exception>
private T ResolveInternal<T>(object[] arguments, List<Type> resolveStack = null) private T ResolveInternal<T>(object[]? arguments, List<Type>? resolveStack = null)
{ {
IRegistration registration = FindRegistration<T>() ?? throw new TypeNotRegisteredException(typeof(T)); IRegistration registration = FindRegistration<T>() ?? throw new TypeNotRegisteredException(typeof(T));
@ -330,11 +329,11 @@ namespace LightweightIocContainer
/// <param name="arguments">The arguments to resolve</param> /// <param name="arguments">The arguments to resolve</param>
/// <param name="resolveStack">The current resolve stack</param> /// <param name="resolveStack">The current resolve stack</param>
/// <returns>An existing or newly created singleton instance of the given <see cref="Type"/></returns> /// <returns>An existing or newly created singleton instance of the given <see cref="Type"/></returns>
private T GetOrCreateSingletonInstance<T>(IRegistration registration, object[] arguments, List<Type> resolveStack) private T GetOrCreateSingletonInstance<T>(IRegistration registration, object[]? arguments, List<Type> resolveStack)
{ {
Type type = GetType<T>(registration); Type type = GetType<T>(registration);
object instance = TryGetSingletonInstance(type); object? instance = TryGetSingletonInstance(type);
if (instance != null) if (instance != null)
return (T) instance; return (T) instance;
@ -350,7 +349,7 @@ namespace LightweightIocContainer
/// </summary> /// </summary>
/// <param name="type">The given <see cref="Type"/></param> /// <param name="type">The given <see cref="Type"/></param>
/// <returns>A singleton instance if existing for the given <see cref="Type"/>, null if not</returns> /// <returns>A singleton instance if existing for the given <see cref="Type"/>, null if not</returns>
private object TryGetSingletonInstance(Type type) => _singletons.FirstOrDefault(s => s.type == type).instance; //if a singleton instance exists return it private object? TryGetSingletonInstance(Type type) => _singletons.FirstOrDefault(s => s.type == type).instance; //if a singleton instance exists return it
/// <summary> /// <summary>
/// Gets or creates a multiton instance of a given <see cref="Type"/> /// Gets or creates a multiton instance of a given <see cref="Type"/>
@ -362,7 +361,7 @@ namespace LightweightIocContainer
/// <returns>An existing or newly created multiton instance of the given <see cref="Type"/></returns> /// <returns>An existing or newly created multiton instance of the given <see cref="Type"/></returns>
/// <exception cref="MultitonResolveException">No arguments given</exception> /// <exception cref="MultitonResolveException">No arguments given</exception>
/// <exception cref="MultitonResolveException">Scope argument not given</exception> /// <exception cref="MultitonResolveException">Scope argument not given</exception>
private T GetOrCreateMultitonInstance<T>(IMultitonRegistration registration, object[] arguments, List<Type> resolveStack) private T GetOrCreateMultitonInstance<T>(IMultitonRegistration registration, object[]? arguments, List<Type> resolveStack)
{ {
if (arguments == null || !arguments.Any()) if (arguments == null || !arguments.Any())
throw new MultitonResolveException("Can not resolve multiton without arguments.", typeof(T)); throw new MultitonResolveException("Can not resolve multiton without arguments.", typeof(T));
@ -375,7 +374,7 @@ namespace LightweightIocContainer
var instances = _multitons.FirstOrDefault(m => m.type == registration.ImplementationType && m.scope == registration.Scope).instances; //get instances for the given type and scope (use implementation type to resolve the correct instance for multiple multiton registrations as well) var instances = _multitons.FirstOrDefault(m => m.type == registration.ImplementationType && m.scope == registration.Scope).instances; //get instances for the given type and scope (use implementation type to resolve the correct instance for multiple multiton registrations as well)
if (instances != null) if (instances != null)
{ {
if (instances.TryGetValue(scopeArgument, out object instance)) if (instances.TryGetValue(scopeArgument, out object? instance) && instance != null)
return (T) instance; return (T) instance;
T createdInstance = CreateInstance<T>(registration, arguments, resolveStack); T createdInstance = CreateInstance<T>(registration, arguments, resolveStack);
@ -386,7 +385,7 @@ namespace LightweightIocContainer
T newInstance = CreateInstance<T>(registration, arguments, resolveStack); T newInstance = CreateInstance<T>(registration, arguments, resolveStack);
ConditionalWeakTable<object, object> weakTable = new(); ConditionalWeakTable<object, object?> weakTable = new();
weakTable.Add(scopeArgument, newInstance); weakTable.Add(scopeArgument, newInstance);
_multitons.Add((registration.ImplementationType, registration.Scope, weakTable)); _multitons.Add((registration.ImplementationType, registration.Scope, weakTable));
@ -402,7 +401,7 @@ namespace LightweightIocContainer
/// <param name="arguments">The constructor arguments</param> /// <param name="arguments">The constructor arguments</param>
/// <param name="resolveStack">The current resolve stack</param> /// <param name="resolveStack">The current resolve stack</param>
/// <returns>A newly created instance of the given <see cref="Type"/></returns> /// <returns>A newly created instance of the given <see cref="Type"/></returns>
private T CreateInstance<T>(IRegistration registration, object[] arguments, List<Type> resolveStack) private T CreateInstance<T>(IRegistration registration, object[]? arguments, List<Type> resolveStack)
{ {
if (registration is IWithParametersInternal { Parameters: { } } registrationWithParameters) if (registration is IWithParametersInternal { Parameters: { } } registrationWithParameters)
arguments = UpdateArgumentsWithRegistrationParameters(registrationWithParameters, arguments); arguments = UpdateArgumentsWithRegistrationParameters(registrationWithParameters, arguments);
@ -450,8 +449,11 @@ namespace LightweightIocContainer
/// <param name="registration">The <see cref="IRegistrationBase"/> of the given <see cref="Type"/></param> /// <param name="registration">The <see cref="IRegistrationBase"/> of the given <see cref="Type"/></param>
/// <param name="arguments">The constructor arguments</param> /// <param name="arguments">The constructor arguments</param>
/// <returns>The argument list updated with the <see cref="IWithParametersInternal.Parameters"/></returns> /// <returns>The argument list updated with the <see cref="IWithParametersInternal.Parameters"/></returns>
private object[] UpdateArgumentsWithRegistrationParameters(IWithParametersInternal registration, object[] arguments) private object[]? UpdateArgumentsWithRegistrationParameters(IWithParametersInternal registration, object[]? arguments)
{ {
if (registration.Parameters == null)
return arguments;
if (arguments != null && arguments.Any()) //if more arguments were passed to resolve if (arguments != null && arguments.Any()) //if more arguments were passed to resolve
{ {
int argumentsSize = registration.Parameters.Length + arguments.Length; int argumentsSize = registration.Parameters.Length + arguments.Length;
@ -495,10 +497,9 @@ namespace LightweightIocContainer
/// <param name="resolveStack">The current resolve stack</param> /// <param name="resolveStack">The current resolve stack</param>
/// <returns>An array of all needed constructor arguments to create the <see cref="Type"/></returns> /// <returns>An array of all needed constructor arguments to create the <see cref="Type"/></returns>
/// <exception cref="NoMatchingConstructorFoundException">No matching constructor was found for the given or resolvable arguments</exception> /// <exception cref="NoMatchingConstructorFoundException">No matching constructor was found for the given or resolvable arguments</exception>
[CanBeNull] private object[]? ResolveTypeCreationArguments(Type type, object[]? arguments, List<Type> resolveStack)
private object[] ResolveTypeCreationArguments(Type type, object[] arguments, List<Type> resolveStack)
{ {
(bool result, List<object> parameters, NoMatchingConstructorFoundException exception) = TryGetTypeResolveStack(type, arguments, resolveStack); (bool result, List<object>? parameters, NoMatchingConstructorFoundException? exception) = TryGetTypeResolveStack(type, arguments, resolveStack);
if (result) if (result)
{ {
@ -534,15 +535,15 @@ namespace LightweightIocContainer
/// <para>parameters: The parameters needed to resolve the given <see cref="Type"/></para> /// <para>parameters: The parameters needed to resolve the given <see cref="Type"/></para>
/// <para>exception: A <see cref="NoMatchingConstructorFoundException"/> if no matching constructor was found</para> /// <para>exception: A <see cref="NoMatchingConstructorFoundException"/> if no matching constructor was found</para>
/// </returns> /// </returns>
private (bool result, List<object> parameters, NoMatchingConstructorFoundException exception) TryGetTypeResolveStack(Type type, object[] arguments, List<Type> resolveStack) private (bool result, List<object>? parameters, NoMatchingConstructorFoundException? exception) TryGetTypeResolveStack(Type type, object[]? arguments, List<Type> resolveStack)
{ {
NoMatchingConstructorFoundException noMatchingConstructorFoundException = null; NoMatchingConstructorFoundException? noMatchingConstructorFoundException = null;
//find best ctor //find best ctor
List<ConstructorInfo> sortedConstructors = TryGetSortedConstructors(type); List<ConstructorInfo> sortedConstructors = TryGetSortedConstructors(type);
foreach (ConstructorInfo constructor in sortedConstructors) foreach (ConstructorInfo constructor in sortedConstructors)
{ {
(bool result, List<object> parameters, List<ConstructorNotMatchingException> exceptions) = TryGetConstructorResolveStack(constructor, arguments, resolveStack); (bool result, List<object>? parameters, List<ConstructorNotMatchingException>? exceptions) = TryGetConstructorResolveStack(constructor, arguments, resolveStack);
if (result) if (result)
return (true, parameters, null); return (true, parameters, null);
@ -566,7 +567,7 @@ namespace LightweightIocContainer
/// <para>parameters: The parameters needed to resolve the given <see cref="Type"/></para> /// <para>parameters: The parameters needed to resolve the given <see cref="Type"/></para>
/// <para>exception: A List of <see cref="ConstructorNotMatchingException"/>s if the constructor is not matching</para> /// <para>exception: A List of <see cref="ConstructorNotMatchingException"/>s if the constructor is not matching</para>
/// </returns> /// </returns>
private (bool result, List<object> parameters, List<ConstructorNotMatchingException> exceptions) TryGetConstructorResolveStack(ConstructorInfo constructor, object[] arguments, List<Type> resolveStack) private (bool result, List<object>? parameters, List<ConstructorNotMatchingException>? exceptions) TryGetConstructorResolveStack(ConstructorInfo constructor, object[]? arguments, List<Type> resolveStack)
{ {
List<ParameterInfo> constructorParameters = constructor.GetParameters().ToList(); List<ParameterInfo> constructorParameters = constructor.GetParameters().ToList();
if (!constructorParameters.Any()) if (!constructorParameters.Any())
@ -575,7 +576,7 @@ namespace LightweightIocContainer
List<ConstructorNotMatchingException> exceptions = new(); List<ConstructorNotMatchingException> exceptions = new();
List<object> parameters = new(); List<object> parameters = new();
List<object> passedArguments = null; List<object>? passedArguments = null;
if (arguments != null) if (arguments != null)
passedArguments = new List<object>(arguments); passedArguments = new List<object>(arguments);
@ -602,21 +603,21 @@ namespace LightweightIocContainer
Type registeredType = GetTypeNonGeneric(parameter.ParameterType, registration); Type registeredType = GetTypeNonGeneric(parameter.ParameterType, registration);
object singletonInstance = TryGetSingletonInstance(registeredType); object? singletonInstance = TryGetSingletonInstance(registeredType);
if (singletonInstance != null) if (singletonInstance != null)
fittingArgument = singletonInstance; fittingArgument = singletonInstance;
else else
{ {
object[] argumentsForRegistration = null; object[]? argumentsForRegistration = null;
if (registration is IWithParametersInternal { Parameters: { } } registrationWithParameters) if (registration is IWithParametersInternal { Parameters: { } } registrationWithParameters)
argumentsForRegistration = UpdateArgumentsWithRegistrationParameters(registrationWithParameters, null); argumentsForRegistration = UpdateArgumentsWithRegistrationParameters(registrationWithParameters, null);
(bool result, List<object> parametersToResolve, NoMatchingConstructorFoundException exception) = (bool result, List<object>? parametersToResolve, NoMatchingConstructorFoundException? exception) =
TryGetTypeResolveStack(registeredType, argumentsForRegistration, internalResolveStack); TryGetTypeResolveStack(registeredType, argumentsForRegistration, internalResolveStack);
if (result) if (result)
fittingArgument = new InternalToBeResolvedPlaceholder(registeredType, parametersToResolve); fittingArgument = new InternalToBeResolvedPlaceholder(registeredType, parametersToResolve);
else else if (exception != null)
exceptions.Add(new ConstructorNotMatchingException(constructor, exception)); exceptions.Add(new ConstructorNotMatchingException(constructor, exception));
} }
} }
@ -648,18 +649,16 @@ namespace LightweightIocContainer
/// </summary> /// </summary>
/// <typeparam name="T">The given <see cref="Type"/></typeparam> /// <typeparam name="T">The given <see cref="Type"/></typeparam>
/// <returns>The <see cref="IRegistration"/> for the given <see cref="Type"/></returns> /// <returns>The <see cref="IRegistration"/> for the given <see cref="Type"/></returns>
[CanBeNull] private IRegistration? FindRegistration<T>() => FindRegistration(typeof(T));
private IRegistration FindRegistration<T>() => FindRegistration(typeof(T));
/// <summary> /// <summary>
/// Find the <see cref="IRegistration"/> for the given <see cref="Type"/> /// Find the <see cref="IRegistration"/> for the given <see cref="Type"/>
/// </summary> /// </summary>
/// <param name="type">The given <see cref="Type"/></param> /// <param name="type">The given <see cref="Type"/></param>
/// <returns>The <see cref="IRegistration"/> for the given <see cref="Type"/></returns> /// <returns>The <see cref="IRegistration"/> for the given <see cref="Type"/></returns>
[CanBeNull] private IRegistration? FindRegistration(Type type)
private IRegistration FindRegistration(Type type)
{ {
IRegistration registration = Registrations.FirstOrDefault(r => r.InterfaceType == type); IRegistration? registration = Registrations.FirstOrDefault(r => r.InterfaceType == type);
if (registration != null) if (registration != null)
return registration; return registration;
@ -721,7 +720,7 @@ namespace LightweightIocContainer
/// <typeparam name="T">The given <see cref="Type"/></typeparam> /// <typeparam name="T">The given <see cref="Type"/></typeparam>
/// <returns>The new resolve stack</returns> /// <returns>The new resolve stack</returns>
/// <exception cref="CircularDependencyException">A circular dependency was detected</exception> /// <exception cref="CircularDependencyException">A circular dependency was detected</exception>
private List<Type> CheckForCircularDependencies<T>(List<Type> resolveStack) => CheckForCircularDependencies(typeof(T), resolveStack); private List<Type> CheckForCircularDependencies<T>(List<Type>? resolveStack) => CheckForCircularDependencies(typeof(T), resolveStack);
/// <summary> /// <summary>
/// Check the given resolve stack for circular dependencies /// Check the given resolve stack for circular dependencies
@ -730,7 +729,7 @@ namespace LightweightIocContainer
/// <param name="resolveStack">The given resolve stack</param> /// <param name="resolveStack">The given resolve stack</param>
/// <returns>The new resolve stack</returns> /// <returns>The new resolve stack</returns>
/// <exception cref="CircularDependencyException">A circular dependency was detected</exception> /// <exception cref="CircularDependencyException">A circular dependency was detected</exception>
private List<Type> CheckForCircularDependencies(Type type, List<Type> resolveStack) private List<Type> CheckForCircularDependencies(Type type, List<Type>? resolveStack)
{ {
if (resolveStack == null) //first resolve call if (resolveStack == null) //first resolve call
resolveStack = new List<Type> {type}; //create new stack and add the currently resolving type to the stack resolveStack = new List<Type> {type}; //create new stack and add the currently resolving type to the stack
@ -748,7 +747,7 @@ namespace LightweightIocContainer
/// <typeparam name="T">The <see cref="Type"/> to clear the multiton instances</typeparam> /// <typeparam name="T">The <see cref="Type"/> to clear the multiton instances</typeparam>
public void ClearMultitonInstances<T>() public void ClearMultitonInstances<T>()
{ {
IRegistration registration = FindRegistration<T>(); IRegistration? registration = FindRegistration<T>();
if (registration is not IMultitonRegistration multitonRegistration) if (registration is not IMultitonRegistration multitonRegistration)
return; return;

@ -14,6 +14,7 @@
<FileVersion>3.0.0.0</FileVersion> <FileVersion>3.0.0.0</FileVersion>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile> <PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<PackageVersion>3.0.0-beta</PackageVersion> <PackageVersion>3.0.0-beta</PackageVersion>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">

@ -213,12 +213,11 @@
No matching constructor was found for the given or resolvable arguments No matching constructor was found for the given or resolvable arguments
</summary> </summary>
</member> </member>
<member name="M:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.#ctor(System.Type,LightweightIocContainer.Exceptions.ConstructorNotMatchingException[])"> <member name="M:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.#ctor(System.Type)">
<summary> <summary>
No matching constructor was found for the given or resolvable arguments No matching constructor was found for the given or resolvable arguments
</summary> </summary>
<param name="type">The <see cref="P:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.Type"/> with no matching constructor</param> <param name="type">The <see cref="P:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.Type"/> with no matching constructor</param>
<param name="exceptions">The inner exceptions of type <see cref="T:LightweightIocContainer.Exceptions.ConstructorNotMatchingException"/></param>
</member> </member>
<member name="P:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.Type"> <member name="P:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.Type">
<summary> <summary>
@ -946,6 +945,14 @@
<returns>An instance of the given <see cref="T:System.Type"/></returns> <returns>An instance of the given <see cref="T:System.Type"/></returns>
<exception cref="T:LightweightIocContainer.Exceptions.InternalResolveException">Could not find function <see cref="M:LightweightIocContainer.IocContainer.ResolveInternal``1(System.Object[],System.Collections.Generic.List{System.Type})"/></exception> <exception cref="T:LightweightIocContainer.Exceptions.InternalResolveException">Could not find function <see cref="M:LightweightIocContainer.IocContainer.ResolveInternal``1(System.Object[],System.Collections.Generic.List{System.Type})"/></exception>
</member> </member>
<member name="M:LightweightIocContainer.IocContainer.Resolve(LightweightIocContainer.ResolvePlaceholders.InternalToBeResolvedPlaceholder,System.Collections.Generic.List{System.Type})">
<summary>
Recursively resolve a <see cref="T:System.Type"/> with the given parameters for an <see cref="T:LightweightIocContainer.ResolvePlaceholders.InternalToBeResolvedPlaceholder"/>
</summary>
<param name="toBeResolvedPlaceholder">The <see cref="T:LightweightIocContainer.ResolvePlaceholders.InternalToBeResolvedPlaceholder"/> that includes the type and resolve stack</param>
<param name="resolveStack">The current resolve stack</param>
<returns>A recursively resolved instance of the given <see cref="T:System.Type"/></returns>
</member>
<member name="M:LightweightIocContainer.IocContainer.ResolveInternal``1(System.Object[],System.Collections.Generic.List{System.Type})"> <member name="M:LightweightIocContainer.IocContainer.ResolveInternal``1(System.Object[],System.Collections.Generic.List{System.Type})">
<summary> <summary>
Gets an instance of a given registered <see cref="T:System.Type"/> Gets an instance of a given registered <see cref="T:System.Type"/>
@ -967,6 +974,13 @@
<param name="resolveStack">The current resolve stack</param> <param name="resolveStack">The current resolve stack</param>
<returns>An existing or newly created singleton instance of the given <see cref="T:System.Type"/></returns> <returns>An existing or newly created singleton instance of the given <see cref="T:System.Type"/></returns>
</member> </member>
<member name="M:LightweightIocContainer.IocContainer.TryGetSingletonInstance(System.Type)">
<summary>
Try to get an existing singleton instance for a given <see cref="T:System.Type"/>
</summary>
<param name="type">The given <see cref="T:System.Type"/></param>
<returns>A singleton instance if existing for the given <see cref="T:System.Type"/>, null if not</returns>
</member>
<member name="M:LightweightIocContainer.IocContainer.GetOrCreateMultitonInstance``1(LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration,System.Object[],System.Collections.Generic.List{System.Type})"> <member name="M:LightweightIocContainer.IocContainer.GetOrCreateMultitonInstance``1(LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration,System.Object[],System.Collections.Generic.List{System.Type})">
<summary> <summary>
Gets or creates a multiton instance of a given <see cref="T:System.Type"/> Gets or creates a multiton instance of a given <see cref="T:System.Type"/>
@ -1007,6 +1021,90 @@
<returns>An array of all needed constructor arguments to create the <see cref="T:System.Type"/></returns> <returns>An array of all needed constructor arguments to create the <see cref="T:System.Type"/></returns>
<exception cref="T:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException">No matching constructor was found for the given or resolvable arguments</exception> <exception cref="T:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException">No matching constructor was found for the given or resolvable arguments</exception>
</member> </member>
<member name="M:LightweightIocContainer.IocContainer.TryGetTypeResolveStack(System.Type,System.Object[],System.Collections.Generic.List{System.Type})">
<summary>
Try to get the resolve stack for a given <see cref="T:System.Type"/>
</summary>
<param name="type">The given <see cref="T:System.Type"/></param>
<param name="arguments">The given arguments</param>
<param name="resolveStack">The current resolve stack</param>
<returns>
<para>result: True if successful, false if not</para>
<para>parameters: The parameters needed to resolve the given <see cref="T:System.Type"/></para>
<para>exception: A <see cref="T:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException"/> if no matching constructor was found</para>
</returns>
</member>
<member name="M:LightweightIocContainer.IocContainer.TryGetConstructorResolveStack(System.Reflection.ConstructorInfo,System.Object[],System.Collections.Generic.List{System.Type})">
<summary>
Try to get the resolve stack for a given constructor
</summary>
<param name="constructor">The <see cref="T:System.Reflection.ConstructorInfo"/> for the given constructor</param>
<param name="arguments">The given arguments</param>
<param name="resolveStack">The current resolve stack</param>
<returns>
<para>result: True if successful, false if not</para>
<para>parameters: The parameters needed to resolve the given <see cref="T:System.Type"/></para>
<para>exception: A List of <see cref="T:LightweightIocContainer.Exceptions.ConstructorNotMatchingException"/>s if the constructor is not matching</para>
</returns>
</member>
<member name="M:LightweightIocContainer.IocContainer.FindRegistration``1">
<summary>
Find the <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/> for the given <see cref="T:System.Type"/>
</summary>
<typeparam name="T">The given <see cref="T:System.Type"/></typeparam>
<returns>The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/> for the given <see cref="T:System.Type"/></returns>
</member>
<member name="M:LightweightIocContainer.IocContainer.FindRegistration(System.Type)">
<summary>
Find the <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/> for the given <see cref="T:System.Type"/>
</summary>
<param name="type">The given <see cref="T:System.Type"/></param>
<returns>The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/> for the given <see cref="T:System.Type"/></returns>
</member>
<member name="M:LightweightIocContainer.IocContainer.TryGetSortedConstructors(System.Type)">
<summary>
Try to get the sorted constructors for the given <see cref="T:System.Type"/>
</summary>
<param name="type">The given <see cref="T:System.Type"/></param>
<returns>A list of sorted <see cref="T:System.Reflection.ConstructorInfo"/> for the given <see cref="T:System.Type"/></returns>
<exception cref="T:LightweightIocContainer.Exceptions.NoPublicConstructorFoundException">No public constructor was found for the given <see cref="T:System.Type"/></exception>
</member>
<member name="M:LightweightIocContainer.IocContainer.GetType``1(LightweightIocContainer.Interfaces.Registrations.IRegistration)">
<summary>
Get the implementation type for the given <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/>
</summary>
<param name="registration">The given <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/></param>
<typeparam name="T">The given <see cref="T:System.Type"/> of the interface</typeparam>
<returns>The implementation <see cref="T:System.Type"/> for the given <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/></returns>
<exception cref="T:LightweightIocContainer.Exceptions.UnknownRegistrationException">Unknown <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/> passed</exception>
</member>
<member name="M:LightweightIocContainer.IocContainer.GetTypeNonGeneric(System.Type,LightweightIocContainer.Interfaces.Registrations.IRegistration)">
<summary>
Non generic method to get the implementation type for the given <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/>
</summary>
<param name="type">The given <see cref="T:System.Type"/> of the interface</param>
<param name="registration">The given <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/></param>
<returns>The implementation <see cref="T:System.Type"/> for the given <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/></returns>
<exception cref="T:LightweightIocContainer.Exceptions.UnknownRegistrationException">Unknown <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/> passed</exception>
</member>
<member name="M:LightweightIocContainer.IocContainer.CheckForCircularDependencies``1(System.Collections.Generic.List{System.Type})">
<summary>
Check the given resolve stack for circular dependencies
</summary>
<param name="resolveStack">The given resolve stack</param>
<typeparam name="T">The given <see cref="T:System.Type"/></typeparam>
<returns>The new resolve stack</returns>
<exception cref="T:LightweightIocContainer.Exceptions.CircularDependencyException">A circular dependency was detected</exception>
</member>
<member name="M:LightweightIocContainer.IocContainer.CheckForCircularDependencies(System.Type,System.Collections.Generic.List{System.Type})">
<summary>
Check the given resolve stack for circular dependencies
</summary>
<param name="type">The given <see cref="T:System.Type"/></param>
<param name="resolveStack">The given resolve stack</param>
<returns>The new resolve stack</returns>
<exception cref="T:LightweightIocContainer.Exceptions.CircularDependencyException">A circular dependency was detected</exception>
</member>
<member name="M:LightweightIocContainer.IocContainer.ClearMultitonInstances``1"> <member name="M:LightweightIocContainer.IocContainer.ClearMultitonInstances``1">
<summary> <summary>
Clear the multiton instances of the given <see cref="T:System.Type"/> from the registered multitons list Clear the multiton instances of the given <see cref="T:System.Type"/> from the registered multitons list
@ -1514,6 +1612,16 @@
An internal placeholder that is used to hold types that need to be resolved during the resolving process An internal placeholder that is used to hold types that need to be resolved during the resolving process
</summary> </summary>
</member> </member>
<member name="P:LightweightIocContainer.ResolvePlaceholders.InternalToBeResolvedPlaceholder.ResolvedType">
<summary>
The <see cref="T:System.Type"/> to be resolved
</summary>
</member>
<member name="P:LightweightIocContainer.ResolvePlaceholders.InternalToBeResolvedPlaceholder.Parameters">
<summary>
The parameters needed to resolve the <see cref="P:LightweightIocContainer.ResolvePlaceholders.InternalToBeResolvedPlaceholder.ResolvedType"/>
</summary>
</member>
<member name="M:LightweightIocContainer.TypeExtension.GetDefault(System.Type)"> <member name="M:LightweightIocContainer.TypeExtension.GetDefault(System.Type)">
<summary> <summary>
Returns the default value for a given <see cref="T:System.Type"/> Returns the default value for a given <see cref="T:System.Type"/>

@ -45,7 +45,7 @@ namespace LightweightIocContainer.Registrations
/// </summary> /// </summary>
/// <param name="action">The <see cref="Action{T}"/></param> /// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action) public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation?> action)
{ {
foreach (IRegistration registration in Registrations) foreach (IRegistration registration in Registrations)
{ {

@ -24,10 +24,8 @@ namespace LightweightIocContainer.Registrations
/// <param name="lifestyle">The <see cref="Lifestyle"/> of this <see cref="MultipleRegistration{TInterface1,TInterface2}"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> of this <see cref="MultipleRegistration{TInterface1,TInterface2}"/></param>
/// <param name="container">The current instance of the <see cref="IIocContainer"/></param> /// <param name="container">The current instance of the <see cref="IIocContainer"/></param>
protected MultipleRegistration(Type interfaceType1, Type implementationType, Lifestyle lifestyle, IocContainer container) protected MultipleRegistration(Type interfaceType1, Type implementationType, Lifestyle lifestyle, IocContainer container)
: base(interfaceType1, implementationType, lifestyle, container) : base(interfaceType1, implementationType, lifestyle, container) =>
{ Registrations = new List<IRegistration>();
}
/// <summary> /// <summary>
/// A <see cref="List{T}"/> of <see cref="IRegistration"/>s that are registered within this <see cref="MultipleRegistration{TInterface1,TInterface2}"/> /// A <see cref="List{T}"/> of <see cref="IRegistration"/>s that are registered within this <see cref="MultipleRegistration{TInterface1,TInterface2}"/>
@ -66,7 +64,7 @@ namespace LightweightIocContainer.Registrations
/// </summary> /// </summary>
/// <param name="action">The <see cref="Action{T}"/></param> /// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action) public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation?> action)
{ {
foreach (IRegistration registration in Registrations) foreach (IRegistration registration in Registrations)
{ {
@ -114,7 +112,7 @@ namespace LightweightIocContainer.Registrations
/// </summary> /// </summary>
/// <param name="action">The <see cref="Action{T}"/></param> /// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action) public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation?> action)
{ {
foreach (IRegistration registration in Registrations) foreach (IRegistration registration in Registrations)
{ {
@ -167,7 +165,7 @@ namespace LightweightIocContainer.Registrations
/// </summary> /// </summary>
/// <param name="action">The <see cref="Action{T}"/></param> /// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action) public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation?> action)
{ {
foreach (IRegistration registration in Registrations) foreach (IRegistration registration in Registrations)
{ {
@ -225,7 +223,7 @@ namespace LightweightIocContainer.Registrations
/// </summary> /// </summary>
/// <param name="action">The <see cref="Action{T}"/></param> /// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action) public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation?> action)
{ {
foreach (IRegistration registration in Registrations) foreach (IRegistration registration in Registrations)
{ {

@ -48,12 +48,12 @@ namespace LightweightIocContainer.Registrations
/// An <see cref="Array"/> of parameters that are used to <see cref="IocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/> /// An <see cref="Array"/> of parameters that are used to <see cref="IocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/>
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="WithParameters(object[])"/></para> /// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="WithParameters(object[])"/></para>
/// </summary> /// </summary>
public object[] Parameters { get; private set; } public object[]? Parameters { get; private set; }
/// <summary> /// <summary>
/// The Factory added with the <see cref="WithFactory{TFactory}"/> method /// The Factory added with the <see cref="WithFactory{TFactory}"/> method
/// </summary> /// </summary>
public ITypedFactory Factory { get; private set; } public ITypedFactory? Factory { get; private set; }
/// <summary> /// <summary>
/// Pass parameters that will be used to <see cref="IocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/> /// Pass parameters that will be used to <see cref="IocContainer.Resolve{T}()"/> an instance of this <see cref="IRegistration.InterfaceType"/>

@ -29,7 +29,7 @@ namespace LightweightIocContainer.Registrations
/// <summary> /// <summary>
/// <see cref="Func{T,TResult}"/> that is invoked instead of creating an instance of this <see cref="Type"/> the default way /// <see cref="Func{T,TResult}"/> that is invoked instead of creating an instance of this <see cref="Type"/> the default way
/// </summary> /// </summary>
public Func<IResolver, T> FactoryMethod { get; private set; } public Func<IResolver, T>? FactoryMethod { get; private set; }
/// <summary> /// <summary>
/// Pass a <see cref="Func{T,TResult}"/> that will be invoked instead of creating an instance of this <see cref="Type"/> the default way /// Pass a <see cref="Func{T,TResult}"/> that will be invoked instead of creating an instance of this <see cref="Type"/> the default way

@ -35,22 +35,22 @@ namespace LightweightIocContainer.Registrations
/// This <see cref="Action"/> is invoked when an instance of this type is created. /// This <see cref="Action"/> is invoked when an instance of this type is created.
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IOnCreate{TInterface,TImplementation}.OnCreate"/></para> /// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IOnCreate{TInterface,TImplementation}.OnCreate"/></para>
/// </summary> /// </summary>
private Action<object> OnCreateAction { get; set; } private Action<object?>? OnCreateAction { get; set; }
/// <summary> /// <summary>
/// This <see cref="Action"/> is invoked when an instance of this type is created. /// This <see cref="Action"/> is invoked when an instance of this type is created.
/// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IOnCreate{TInterface,TImplementation}.OnCreate"/></para> /// <para>Can be set in the <see cref="IIocInstaller"/> by calling <see cref="IOnCreate{TInterface,TImplementation}.OnCreate"/></para>
/// </summary> /// </summary>
Action<object> IOnCreate.OnCreateAction => OnCreateAction; Action<object?>? IOnCreate.OnCreateAction => OnCreateAction;
/// <summary> /// <summary>
/// Pass an <see cref="Action{T}"/> that will be invoked when an instance of this type is created /// Pass an <see cref="Action{T}"/> that will be invoked when an instance of this type is created
/// </summary> /// </summary>
/// <param name="action">The <see cref="Action{T}"/></param> /// <param name="action">The <see cref="Action{T}"/></param>
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public virtual ITypedRegistration<TInterface, TImplementation> OnCreate(Action<TImplementation> action) public virtual ITypedRegistration<TInterface, TImplementation> OnCreate(Action<TImplementation?> action)
{ {
OnCreateAction = a => action((TImplementation) a); OnCreateAction = a => action((TImplementation?) a);
return this; return this;
} }
} }

@ -12,7 +12,7 @@ namespace LightweightIocContainer.ResolvePlaceholders
/// </summary> /// </summary>
internal class InternalToBeResolvedPlaceholder internal class InternalToBeResolvedPlaceholder
{ {
public InternalToBeResolvedPlaceholder(Type resolvedType, List<object> parameters) public InternalToBeResolvedPlaceholder(Type resolvedType, List<object>? parameters)
{ {
ResolvedType = resolvedType; ResolvedType = resolvedType;
Parameters = parameters; Parameters = parameters;
@ -26,6 +26,6 @@ namespace LightweightIocContainer.ResolvePlaceholders
/// <summary> /// <summary>
/// The parameters needed to resolve the <see cref="ResolvedType"/> /// The parameters needed to resolve the <see cref="ResolvedType"/>
/// </summary> /// </summary>
public List<object> Parameters { get; } public List<object>? Parameters { get; }
} }
} }

@ -13,6 +13,6 @@ namespace LightweightIocContainer
/// </summary> /// </summary>
/// <param name="type">The given <see cref="Type"/></param> /// <param name="type">The given <see cref="Type"/></param>
/// <returns>The default value for the given <see cref="Type"/></returns> /// <returns>The default value for the given <see cref="Type"/></returns>
public static object GetDefault(this Type type) => type.IsValueType ? Activator.CreateInstance(type) : null; public static object? GetDefault(this Type type) => type.IsValueType ? Activator.CreateInstance(type) : null;
} }
} }

@ -16,7 +16,7 @@ namespace LightweightIocContainer.Validation
public class IocValidator public class IocValidator
{ {
private readonly IocContainer _iocContainer; private readonly IocContainer _iocContainer;
private readonly List<(Type type, object parameter)> _parameters; private readonly List<(Type type, object? parameter)> _parameters;
/// <summary> /// <summary>
/// Validator for your <see cref="IocContainer"/> to check if everything can be resolved with your current setup /// Validator for your <see cref="IocContainer"/> to check if everything can be resolved with your current setup
@ -25,7 +25,7 @@ namespace LightweightIocContainer.Validation
public IocValidator(IocContainer iocContainer) public IocValidator(IocContainer iocContainer)
{ {
_iocContainer = iocContainer; _iocContainer = iocContainer;
_parameters = new List<(Type, object)>(); _parameters = new List<(Type, object?)>();
} }
/// <summary> /// <summary>
@ -67,7 +67,7 @@ namespace LightweightIocContainer.Validation
throw new AggregateException("Validation failed.", validationExceptions); throw new AggregateException("Validation failed.", validationExceptions);
} }
private void TryResolve(Type type, object[] arguments, List<Exception> validationExceptions) private void TryResolve(Type type, object?[]? arguments, List<Exception> validationExceptions)
{ {
try try
{ {

Loading…
Cancel
Save