- use collection initializer

ImplementExpressionTrees
Simon G 2 years ago
parent dd658b2b83
commit 0da0dbab7c
Signed by: SimonG
GPG Key ID: 0B82B964BA536523
  1. 6
      LightweightIocContainer/Factories/TypedFactory.cs
  2. 26
      LightweightIocContainer/IocContainer.cs
  3. 26
      LightweightIocContainer/Registrations/MultipleRegistration.cs
  4. 12
      Test.LightweightIocContainer/AssemblyInstallerTest.cs
  5. 32
      Test.LightweightIocContainer/EnumerableExtensionTest.cs

@ -51,7 +51,7 @@ public class TypedFactory<TFactory> : TypedFactoryBase<TFactory>, ITypedFactory<
FieldBuilder helperFieldBuilder = typeBuilder.DefineField("_helper", typeof(FactoryHelper), FieldAttributes.Private | FieldAttributes.InitOnly);
//add ctor
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new[] {typeof(IocContainer), typeof(FactoryHelper)});
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, [typeof(IocContainer), typeof(FactoryHelper)]);
ILGenerator constructorGenerator = constructorBuilder.GetILGenerator();
constructorGenerator.Emit(OpCodes.Ldarg_0);
constructorGenerator.Emit(OpCodes.Ldarg_1);
@ -129,13 +129,13 @@ public class TypedFactory<TFactory> : TypedFactoryBase<TFactory>, ITypedFactory<
generator.EmitCall(OpCodes.Call, emptyArray, null);
}
generator.EmitCall(OpCodes.Call, typeof(FactoryHelper).GetMethod(nameof(FactoryHelper.ConvertPassedNull), new[] { typeof(MethodBase), typeof(object?[]) })!, null);
generator.EmitCall(OpCodes.Call, typeof(FactoryHelper).GetMethod(nameof(FactoryHelper.ConvertPassedNull), [typeof(MethodBase), typeof(object?[])])!, null);
generator.Emit(OpCodes.Stloc_1);
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld, containerFieldBuilder);
generator.Emit(OpCodes.Ldloc_1);
generator.EmitCall(OpCodes.Call, typeof(IocContainer).GetMethod(nameof(IocContainer.FactoryResolve), new[] { typeof(object?[]) })!.MakeGenericMethod(createMethod.ReturnType), null);
generator.EmitCall(OpCodes.Call, typeof(IocContainer).GetMethod(nameof(IocContainer.FactoryResolve), [typeof(object?[])])!.MakeGenericMethod(createMethod.ReturnType), null);
generator.Emit(OpCodes.Castclass, createMethod.ReturnType);
generator.Emit(OpCodes.Ret);
}

@ -22,8 +22,8 @@ public class IocContainer : IIocContainer, IIocResolver
{
private readonly RegistrationFactory _registrationFactory;
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, object? instance)> _singletons = [];
private readonly List<(Type type, Type scope, ConditionalWeakTable<object, object?> instances)> _multitons = [];
private readonly List<Type> _ignoreConstructorAttributes;
@ -33,8 +33,8 @@ public class IocContainer : IIocContainer, IIocResolver
public IocContainer()
{
_registrationFactory = new RegistrationFactory(this);
_ignoreConstructorAttributes = new List<Type>();
Registrations = new List<IRegistration>();
_ignoreConstructorAttributes = [];
Registrations = [];
}
internal List<IRegistration> Registrations { get; }
@ -161,7 +161,7 @@ public class IocContainer : IIocContainer, IIocResolver
if (registration == null)
return (false, new object(), new TypeNotRegisteredException(typeof(T)));
List<Type> internalResolveStack = resolveStack == null ? new List<Type>() : new List<Type>(resolveStack);
List<Type> internalResolveStack = resolveStack == null ? [] : [..resolveStack];
(bool success, internalResolveStack, CircularDependencyException? circularDependencyException) = CheckForCircularDependencies<T>(internalResolveStack);
if (!success && circularDependencyException is not null)
@ -198,7 +198,7 @@ public class IocContainer : IIocContainer, IIocResolver
object multitonScopeArgument = TryGetMultitonScopeArgument(multitonRegistration, arguments);
parametersToResolve ??= new List<object?>();
parametersToResolve ??= [];
parametersToResolve.Insert(0, multitonScopeArgument); //insert scope at first place, won't be passed to ctor when creating multiton
}
@ -233,7 +233,7 @@ public class IocContainer : IIocContainer, IIocResolver
if (genericMethod == null)
throw new GenericMethodNotFoundException(nameof(TryResolve));
object? resolvedValue = genericMethod.Invoke(this, new object?[]{arguments, resolveStack, isFactoryResolve});
object? resolvedValue = genericMethod.Invoke(this, [arguments, resolveStack, isFactoryResolve]);
if (resolvedValue is not ValueTuple<bool, object, Exception?> resolvedTuple)
throw new Exception("Invalid return value!");
@ -255,7 +255,7 @@ public class IocContainer : IIocContainer, IIocResolver
if (toBeResolvedPlaceholder.Parameters == null)
return CreateInstance<T>(toBeResolvedPlaceholder.ResolvedRegistration, null);
List<object?> parameters = new();
List<object?> parameters = [];
foreach (object? parameter in toBeResolvedPlaceholder.Parameters)
{
if (parameter != null)
@ -519,12 +519,12 @@ public class IocContainer : IIocContainer, IIocResolver
{
List<ParameterInfo> constructorParameters = constructor.GetParameters().ToList();
List<ConstructorNotMatchingException> exceptions = new();
List<object?> parameters = new();
List<ConstructorNotMatchingException> exceptions = [];
List<object?> parameters = [];
List<object?>? passedArguments = null;
if (arguments != null)
passedArguments = new List<object?>(arguments);
passedArguments = [..arguments];
foreach (ParameterInfo parameter in constructorParameters)
{
@ -664,9 +664,9 @@ public class IocContainer : IIocContainer, IIocResolver
private (bool success, List<Type> resolveStack, CircularDependencyException? exception) CheckForCircularDependencies<T>(List<Type>? resolveStack)
{
if (resolveStack == null) //first resolve call
resolveStack = new List<Type> {typeof(T)}; //create new stack and add the currently resolving type to the stack
resolveStack = [typeof(T)]; //create new stack and add the currently resolving type to the stack
else if (resolveStack.Contains(typeof(T)))
return (false, new List<Type>(), new CircularDependencyException(typeof(T), resolveStack)); //currently resolving type is still resolving -> circular dependency
return (false, [], new CircularDependencyException(typeof(T), resolveStack)); //currently resolving type is still resolving -> circular dependency
else //not the first resolve call in chain but no circular dependencies for now
resolveStack.Add(typeof(T)); //add currently resolving type to the stack

@ -23,7 +23,7 @@ internal abstract class MultipleRegistration<TInterface1, TImplementation> : Typ
/// <param name="container">The current instance of the <see cref="IIocContainer"/></param>
protected MultipleRegistration(Type interfaceType1, Type implementationType, Lifestyle lifestyle, IocContainer container)
: base(interfaceType1, implementationType, lifestyle, container) =>
Registrations = new List<IRegistration>();
Registrations = [];
/// <summary>
/// A <see cref="List{T}"/> of <see cref="IRegistration"/>s that are registered within this <see cref="MultipleRegistration{TInterface1,TInterface2}"/>
@ -50,11 +50,11 @@ internal class MultipleRegistration<TInterface1, TInterface2, TImplementation> :
public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type implementationType, Lifestyle lifestyle, IocContainer container)
: base(interfaceType1, implementationType, lifestyle, container)
{
Registrations = new List<IRegistration>
{
Registrations =
[
new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container),
new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container)
};
];
}
/// <summary>
@ -97,12 +97,12 @@ internal class MultipleRegistration<TInterface1, TInterface2, TInterface3, TImpl
public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type interfaceType3, Type implementationType, Lifestyle lifestyle, IocContainer container)
: base(interfaceType1, implementationType, lifestyle, container)
{
Registrations = new List<IRegistration>
{
Registrations =
[
new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container),
new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container),
new TypedRegistration<TInterface3, TImplementation>(interfaceType3, implementationType, lifestyle, container)
};
];
}
/// <summary>
@ -149,13 +149,13 @@ internal class MultipleRegistration<TInterface1, TInterface2, TInterface3, TInte
public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type interfaceType3, Type interfaceType4, Type implementationType, Lifestyle lifestyle, IocContainer container)
: base(interfaceType1, implementationType, lifestyle, container)
{
Registrations = new List<IRegistration>
{
Registrations =
[
new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container),
new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container),
new TypedRegistration<TInterface3, TImplementation>(interfaceType3, implementationType, lifestyle, container),
new TypedRegistration<TInterface4, TImplementation>(interfaceType4, implementationType, lifestyle, container)
};
];
}
/// <summary>
@ -206,14 +206,14 @@ internal class MultipleRegistration<TInterface1, TInterface2, TInterface3, TInte
public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type interfaceType3, Type interfaceType4, Type interfaceType5, Type implementationType, Lifestyle lifestyle, IocContainer container)
: base(interfaceType1, implementationType, lifestyle, container)
{
Registrations = new List<IRegistration>
{
Registrations =
[
new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container),
new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container),
new TypedRegistration<TInterface3, TImplementation>(interfaceType3, implementationType, lifestyle, container),
new TypedRegistration<TInterface4, TImplementation>(interfaceType4, implementationType, lifestyle, container),
new TypedRegistration<TInterface5, TImplementation>(interfaceType5, implementationType, lifestyle, container)
};
];
}
/// <summary>

@ -33,11 +33,11 @@ public class AssemblyInstallerTest
[Test]
public void TestInstall()
{
List<Type> types = new()
{
List<Type> types =
[
typeof(object),
typeof(TestInstaller)
};
];
AssemblyWrapper assemblyMock = Substitute.For<AssemblyWrapper>();
assemblyMock.GetTypes().Returns(types.ToArray());
@ -60,11 +60,11 @@ public class AssemblyInstallerTest
[Test]
public void TestFromAssemblyInstance()
{
List<Type> types = new()
{
List<Type> types =
[
typeof(object),
typeof(TestInstaller)
};
];
AssemblyWrapper assemblyMock = Substitute.For<AssemblyWrapper>();
assemblyMock.GetTypes().Returns(types.ToArray());

@ -34,20 +34,20 @@ public class EnumerableExtensionTest
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")]
public void TestFirstOrGivenNoPredicateEmpty()
{
List<ListObject> list = new();
List<ListObject> list = [];
Assert.That(list.FirstOrGiven<ListObject, Given>(), Is.InstanceOf<Given>());
}
[Test]
public void TestFirstOrGivenNoPredicate()
{
List<ListObject> list = new()
{
new ListObject {Index = 0},
new ListObject {Index = 1},
new ListObject {Index = 2},
new ListObject {Index = 3}
};
List<ListObject> list =
[
new ListObject { Index = 0 },
new ListObject { Index = 1 },
new ListObject { Index = 2 },
new ListObject { Index = 3 }
];
ListObject listObject = list.FirstOrGiven<ListObject, Given>();
@ -59,20 +59,20 @@ public class EnumerableExtensionTest
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")]
public void TestFirstOrGivenPredicateEmpty()
{
List<ListObject> list = new();
List<ListObject> list = [];
Assert.That(list.FirstOrGiven<ListObject, Given>(o => o.Index == 2), Is.InstanceOf<Given>());
}
[Test]
public void TestFirstOrGivenPredicate()
{
List<ListObject> list = new()
{
new ListObject {Index = 0},
new ListObject {Index = 1},
new ListObject {Index = 2},
new ListObject {Index = 3}
};
List<ListObject> list =
[
new ListObject { Index = 0 },
new ListObject { Index = 1 },
new ListObject { Index = 2 },
new ListObject { Index = 3 }
];
ListObject listObject = list.FirstOrGiven<ListObject, Given>(o => o.Index == 2);

Loading…
Cancel
Save