- fix some resharper issues

pull/32/head
Simon Gockner 7 years ago
parent b779e0ccba
commit 0e85f2fcf4
  1. 22
      LightweightIocContainer/IocContainer.cs
  2. 9
      LightweightIocContainer/Registrations/TypedFactoryRegistration.cs

@ -80,8 +80,8 @@ namespace LightweightIocContainer
/// <exception cref="InternalResolveException">Could not find function <see cref="ResolveInternal{T}"/></exception> /// <exception cref="InternalResolveException">Could not find function <see cref="ResolveInternal{T}"/></exception>
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) //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); MethodInfo resolveMethod = typeof(IocContainer).GetMethod(nameof(ResolveInternal), BindingFlags.NonPublic | BindingFlags.Instance);
var genericResolveMethod = resolveMethod?.MakeGenericMethod(type); MethodInfo genericResolveMethod = resolveMethod?.MakeGenericMethod(type);
if (genericResolveMethod == null) if (genericResolveMethod == null)
throw new InternalResolveException($"Could not find function {nameof(ResolveInternal)}"); throw new InternalResolveException($"Could not find function {nameof(ResolveInternal)}");
@ -106,11 +106,11 @@ namespace LightweightIocContainer
if (registration is IDefaultRegistration<T> defaultRegistration) if (registration is IDefaultRegistration<T> defaultRegistration)
{ {
if (defaultRegistration.Lifestyle == Lifestyle.Singleton) if (defaultRegistration.Lifestyle == Lifestyle.Singleton)
return GetOrCreateSingletonInstance<T>(defaultRegistration, arguments); return GetOrCreateSingletonInstance(defaultRegistration, arguments);
else if (defaultRegistration is IMultitonRegistration<T> multitonRegistration && defaultRegistration.Lifestyle == Lifestyle.Multiton) else if (defaultRegistration is IMultitonRegistration<T> multitonRegistration && defaultRegistration.Lifestyle == Lifestyle.Multiton)
return GetOrCreateMultitonInstance<T>(multitonRegistration, arguments); return GetOrCreateMultitonInstance(multitonRegistration, arguments);
return CreateInstance<T>(defaultRegistration, arguments); return CreateInstance(defaultRegistration, arguments);
} }
else if (registration is ITypedFactoryRegistration<T> typedFactoryRegistration) else if (registration is ITypedFactoryRegistration<T> typedFactoryRegistration)
{ {
@ -135,7 +135,7 @@ namespace LightweightIocContainer
return (T) instance; return (T) instance;
//if it doesn't already exist create a new instance and add it to the list //if it doesn't already exist create a new instance and add it to the list
T newInstance = CreateInstance<T>(registration, arguments); T newInstance = CreateInstance(registration, arguments);
_singletons.Add((typeof(T), newInstance)); _singletons.Add((typeof(T), newInstance));
return newInstance; return newInstance;
@ -167,14 +167,14 @@ namespace LightweightIocContainer
if (instance != (null, null)) if (instance != (null, null))
return (T) instance.instance; return (T) instance.instance;
T createdInstance = CreateInstance<T>(registration, arguments); T createdInstance = CreateInstance(registration, arguments);
instances.Add((scopeArgument, createdInstance)); instances.Add((scopeArgument, createdInstance));
return createdInstance; return createdInstance;
} }
T newInstance = CreateInstance<T>(registration, arguments); T newInstance = CreateInstance(registration, arguments);
_multitons.Add((typeof(T), registration.Scope, new List<(object, object)>() {(scopeArgument, newInstance)})); _multitons.Add((typeof(T), registration.Scope, new List<(object, object)> {(scopeArgument, newInstance)}));
return newInstance; return newInstance;
} }
@ -204,7 +204,7 @@ namespace LightweightIocContainer
private object[] ResolveConstructorArguments(Type type, object[] arguments) private object[] ResolveConstructorArguments(Type type, object[] arguments)
{ {
//find best ctor //find best ctor
var sortedCtors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length); IOrderedEnumerable<ConstructorInfo> sortedCtors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length);
foreach (var ctor in sortedCtors) foreach (var ctor in sortedCtors)
{ {
try try
@ -212,7 +212,7 @@ namespace LightweightIocContainer
List<object> argumentsList = arguments?.ToList(); List<object> argumentsList = arguments?.ToList();
List<object> ctorParams = new List<object>(); List<object> ctorParams = new List<object>();
var parameters = ctor.GetParameters(); ParameterInfo[] parameters = ctor.GetParameters();
foreach (var parameter in parameters) foreach (var parameter in parameters)
{ {
object fittingArgument = null; object fittingArgument = null;

@ -3,6 +3,7 @@
// Copyright(c) 2019 SimonG. All Rights Reserved. // Copyright(c) 2019 SimonG. All Rights Reserved.
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Reflection.Emit; using System.Reflection.Emit;
@ -50,7 +51,7 @@ namespace LightweightIocContainer.Registrations
/// <exception cref="InvalidFactoryRegistrationException">Factory registration is invalid</exception> /// <exception cref="InvalidFactoryRegistrationException">Factory registration is invalid</exception>
private void CreateFactory(IIocContainer container) private void CreateFactory(IIocContainer container)
{ {
var createMethods = InterfaceType.GetMethods().Where(m => m.ReturnType != typeof(void)).ToList(); List<MethodInfo> createMethods = InterfaceType.GetMethods().Where(m => m.ReturnType != typeof(void)).ToList();
if (!createMethods.Any()) if (!createMethods.Any())
throw new InvalidFactoryRegistrationException($"Factory {Name} has no create methods."); throw new InvalidFactoryRegistrationException($"Factory {Name} has no create methods.");
@ -70,7 +71,7 @@ namespace LightweightIocContainer.Registrations
//add ctor //add ctor
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new[] {typeof(IIocContainer)}); 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_0);
constructorGenerator.Emit(OpCodes.Ldarg_1); constructorGenerator.Emit(OpCodes.Ldarg_1);
constructorGenerator.Emit(OpCodes.Stfld, containerFieldBuilder); //set `_container` field constructorGenerator.Emit(OpCodes.Stfld, containerFieldBuilder); //set `_container` field
@ -84,13 +85,13 @@ namespace LightweightIocContainer.Registrations
// return IIocContainer.Resolve(`createMethod.ReturnType`, params); // return IIocContainer.Resolve(`createMethod.ReturnType`, params);
//} //}
var args = createMethod.GetParameters(); ParameterInfo[] args = createMethod.GetParameters();
MethodBuilder methodBuilder = typeBuilder.DefineMethod(createMethod.Name, MethodAttributes.Public | MethodAttributes.Virtual, MethodBuilder methodBuilder = typeBuilder.DefineMethod(createMethod.Name, MethodAttributes.Public | MethodAttributes.Virtual,
createMethod.ReturnType, (from arg in args select arg.ParameterType).ToArray()); createMethod.ReturnType, (from arg in args select arg.ParameterType).ToArray());
typeBuilder.DefineMethodOverride(methodBuilder, createMethod); typeBuilder.DefineMethodOverride(methodBuilder, createMethod);
var generator = methodBuilder.GetILGenerator(); ILGenerator generator = methodBuilder.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld, containerFieldBuilder); generator.Emit(OpCodes.Ldfld, containerFieldBuilder);

Loading…
Cancel
Save