diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index 9766856..7ecba5c 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -80,8 +80,8 @@ namespace LightweightIocContainer /// 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 { - var resolveMethod = typeof(IocContainer).GetMethod(nameof(ResolveInternal), BindingFlags.NonPublic | BindingFlags.Instance); - var genericResolveMethod = resolveMethod?.MakeGenericMethod(type); + MethodInfo resolveMethod = typeof(IocContainer).GetMethod(nameof(ResolveInternal), BindingFlags.NonPublic | BindingFlags.Instance); + MethodInfo genericResolveMethod = resolveMethod?.MakeGenericMethod(type); if (genericResolveMethod == null) throw new InternalResolveException($"Could not find function {nameof(ResolveInternal)}"); @@ -106,11 +106,11 @@ namespace LightweightIocContainer if (registration is IDefaultRegistration defaultRegistration) { if (defaultRegistration.Lifestyle == Lifestyle.Singleton) - return GetOrCreateSingletonInstance(defaultRegistration, arguments); + return GetOrCreateSingletonInstance(defaultRegistration, arguments); else if (defaultRegistration is IMultitonRegistration multitonRegistration && defaultRegistration.Lifestyle == Lifestyle.Multiton) - return GetOrCreateMultitonInstance(multitonRegistration, arguments); + return GetOrCreateMultitonInstance(multitonRegistration, arguments); - return CreateInstance(defaultRegistration, arguments); + return CreateInstance(defaultRegistration, arguments); } else if (registration is ITypedFactoryRegistration typedFactoryRegistration) { @@ -135,7 +135,7 @@ namespace LightweightIocContainer return (T) instance; //if it doesn't already exist create a new instance and add it to the list - T newInstance = CreateInstance(registration, arguments); + T newInstance = CreateInstance(registration, arguments); _singletons.Add((typeof(T), newInstance)); return newInstance; @@ -167,14 +167,14 @@ namespace LightweightIocContainer if (instance != (null, null)) return (T) instance.instance; - T createdInstance = CreateInstance(registration, arguments); + T createdInstance = CreateInstance(registration, arguments); instances.Add((scopeArgument, createdInstance)); return createdInstance; } - T newInstance = CreateInstance(registration, arguments); - _multitons.Add((typeof(T), registration.Scope, new List<(object, object)>() {(scopeArgument, newInstance)})); + T newInstance = CreateInstance(registration, arguments); + _multitons.Add((typeof(T), registration.Scope, new List<(object, object)> {(scopeArgument, newInstance)})); return newInstance; } @@ -204,7 +204,7 @@ namespace LightweightIocContainer private object[] ResolveConstructorArguments(Type type, object[] arguments) { //find best ctor - var sortedCtors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length); + IOrderedEnumerable sortedCtors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length); foreach (var ctor in sortedCtors) { try @@ -212,7 +212,7 @@ namespace LightweightIocContainer List argumentsList = arguments?.ToList(); List ctorParams = new List(); - var parameters = ctor.GetParameters(); + ParameterInfo[] parameters = ctor.GetParameters(); foreach (var parameter in parameters) { object fittingArgument = null; diff --git a/LightweightIocContainer/Registrations/TypedFactoryRegistration.cs b/LightweightIocContainer/Registrations/TypedFactoryRegistration.cs index 0e00fba..9978ef0 100644 --- a/LightweightIocContainer/Registrations/TypedFactoryRegistration.cs +++ b/LightweightIocContainer/Registrations/TypedFactoryRegistration.cs @@ -3,6 +3,7 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Reflection.Emit; @@ -50,7 +51,7 @@ namespace LightweightIocContainer.Registrations /// Factory registration is invalid private void CreateFactory(IIocContainer container) { - var createMethods = InterfaceType.GetMethods().Where(m => m.ReturnType != typeof(void)).ToList(); + List createMethods = InterfaceType.GetMethods().Where(m => m.ReturnType != typeof(void)).ToList(); if (!createMethods.Any()) throw new InvalidFactoryRegistrationException($"Factory {Name} has no create methods."); @@ -70,7 +71,7 @@ namespace LightweightIocContainer.Registrations //add ctor ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new[] {typeof(IIocContainer)}); - var constructorGenerator = constructorBuilder.GetILGenerator(); + ILGenerator constructorGenerator = constructorBuilder.GetILGenerator(); constructorGenerator.Emit(OpCodes.Ldarg_0); constructorGenerator.Emit(OpCodes.Ldarg_1); constructorGenerator.Emit(OpCodes.Stfld, containerFieldBuilder); //set `_container` field @@ -84,13 +85,13 @@ namespace LightweightIocContainer.Registrations // return IIocContainer.Resolve(`createMethod.ReturnType`, params); //} - var args = createMethod.GetParameters(); + ParameterInfo[] args = createMethod.GetParameters(); MethodBuilder methodBuilder = typeBuilder.DefineMethod(createMethod.Name, MethodAttributes.Public | MethodAttributes.Virtual, createMethod.ReturnType, (from arg in args select arg.ParameterType).ToArray()); typeBuilder.DefineMethodOverride(methodBuilder, createMethod); - var generator = methodBuilder.GetILGenerator(); + ILGenerator generator = methodBuilder.GetILGenerator(); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldfld, containerFieldBuilder);