diff --git a/LightweightIocContainer/Factories/TypedFactory.cs b/LightweightIocContainer/Factories/TypedFactory.cs index 8a63b01..66e89ee 100644 --- a/LightweightIocContainer/Factories/TypedFactory.cs +++ b/LightweightIocContainer/Factories/TypedFactory.cs @@ -51,7 +51,7 @@ public class TypedFactory : TypedFactoryBase, 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 : TypedFactoryBase, 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); } diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index 2ab8847..2125898 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -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 instances)> _multitons = new(); + private readonly List<(Type type, object? instance)> _singletons = []; + private readonly List<(Type type, Type scope, ConditionalWeakTable instances)> _multitons = []; private readonly List _ignoreConstructorAttributes; @@ -33,8 +33,8 @@ public class IocContainer : IIocContainer, IIocResolver public IocContainer() { _registrationFactory = new RegistrationFactory(this); - _ignoreConstructorAttributes = new List(); - Registrations = new List(); + _ignoreConstructorAttributes = []; + Registrations = []; } internal List Registrations { get; } @@ -161,7 +161,7 @@ public class IocContainer : IIocContainer, IIocResolver if (registration == null) return (false, new object(), new TypeNotRegisteredException(typeof(T))); - List internalResolveStack = resolveStack == null ? new List() : new List(resolveStack); + List internalResolveStack = resolveStack == null ? [] : [..resolveStack]; (bool success, internalResolveStack, CircularDependencyException? circularDependencyException) = CheckForCircularDependencies(internalResolveStack); if (!success && circularDependencyException is not null) @@ -198,7 +198,7 @@ public class IocContainer : IIocContainer, IIocResolver object multitonScopeArgument = TryGetMultitonScopeArgument(multitonRegistration, arguments); - parametersToResolve ??= new List(); + 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 resolvedTuple) throw new Exception("Invalid return value!"); @@ -255,7 +255,7 @@ public class IocContainer : IIocContainer, IIocResolver if (toBeResolvedPlaceholder.Parameters == null) return CreateInstance(toBeResolvedPlaceholder.ResolvedRegistration, null); - List parameters = new(); + List parameters = []; foreach (object? parameter in toBeResolvedPlaceholder.Parameters) { if (parameter != null) @@ -519,12 +519,12 @@ public class IocContainer : IIocContainer, IIocResolver { List constructorParameters = constructor.GetParameters().ToList(); - List exceptions = new(); - List parameters = new(); + List exceptions = []; + List parameters = []; List? passedArguments = null; if (arguments != null) - passedArguments = new List(arguments); + passedArguments = [..arguments]; foreach (ParameterInfo parameter in constructorParameters) { @@ -664,9 +664,9 @@ public class IocContainer : IIocContainer, IIocResolver private (bool success, List resolveStack, CircularDependencyException? exception) CheckForCircularDependencies(List? resolveStack) { if (resolveStack == null) //first resolve call - resolveStack = new List {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(), 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 diff --git a/LightweightIocContainer/Registrations/MultipleRegistration.cs b/LightweightIocContainer/Registrations/MultipleRegistration.cs index 1ad8948..e8d46b9 100644 --- a/LightweightIocContainer/Registrations/MultipleRegistration.cs +++ b/LightweightIocContainer/Registrations/MultipleRegistration.cs @@ -23,7 +23,7 @@ internal abstract class MultipleRegistration : Typ /// The current instance of the protected MultipleRegistration(Type interfaceType1, Type implementationType, Lifestyle lifestyle, IocContainer container) : base(interfaceType1, implementationType, lifestyle, container) => - Registrations = new List(); + Registrations = []; /// /// A of s that are registered within this @@ -50,11 +50,11 @@ internal class MultipleRegistration : public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type implementationType, Lifestyle lifestyle, IocContainer container) : base(interfaceType1, implementationType, lifestyle, container) { - Registrations = new List - { + Registrations = + [ new TypedRegistration(interfaceType1, implementationType, lifestyle, container), new TypedRegistration(interfaceType2, implementationType, lifestyle, container) - }; + ]; } /// @@ -97,12 +97,12 @@ internal class MultipleRegistration - { + Registrations = + [ new TypedRegistration(interfaceType1, implementationType, lifestyle, container), new TypedRegistration(interfaceType2, implementationType, lifestyle, container), new TypedRegistration(interfaceType3, implementationType, lifestyle, container) - }; + ]; } /// @@ -149,13 +149,13 @@ internal class MultipleRegistration - { + Registrations = + [ new TypedRegistration(interfaceType1, implementationType, lifestyle, container), new TypedRegistration(interfaceType2, implementationType, lifestyle, container), new TypedRegistration(interfaceType3, implementationType, lifestyle, container), new TypedRegistration(interfaceType4, implementationType, lifestyle, container) - }; + ]; } /// @@ -206,14 +206,14 @@ internal class MultipleRegistration - { + Registrations = + [ new TypedRegistration(interfaceType1, implementationType, lifestyle, container), new TypedRegistration(interfaceType2, implementationType, lifestyle, container), new TypedRegistration(interfaceType3, implementationType, lifestyle, container), new TypedRegistration(interfaceType4, implementationType, lifestyle, container), new TypedRegistration(interfaceType5, implementationType, lifestyle, container) - }; + ]; } /// diff --git a/Test.LightweightIocContainer/AssemblyInstallerTest.cs b/Test.LightweightIocContainer/AssemblyInstallerTest.cs index 2726cfa..a745046 100644 --- a/Test.LightweightIocContainer/AssemblyInstallerTest.cs +++ b/Test.LightweightIocContainer/AssemblyInstallerTest.cs @@ -33,11 +33,11 @@ public class AssemblyInstallerTest [Test] public void TestInstall() { - List types = new() - { + List types = + [ typeof(object), typeof(TestInstaller) - }; + ]; AssemblyWrapper assemblyMock = Substitute.For(); assemblyMock.GetTypes().Returns(types.ToArray()); @@ -60,11 +60,11 @@ public class AssemblyInstallerTest [Test] public void TestFromAssemblyInstance() { - List types = new() - { + List types = + [ typeof(object), typeof(TestInstaller) - }; + ]; AssemblyWrapper assemblyMock = Substitute.For(); assemblyMock.GetTypes().Returns(types.ToArray()); diff --git a/Test.LightweightIocContainer/EnumerableExtensionTest.cs b/Test.LightweightIocContainer/EnumerableExtensionTest.cs index 7f1627e..b27da42 100644 --- a/Test.LightweightIocContainer/EnumerableExtensionTest.cs +++ b/Test.LightweightIocContainer/EnumerableExtensionTest.cs @@ -34,20 +34,20 @@ public class EnumerableExtensionTest [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")] public void TestFirstOrGivenNoPredicateEmpty() { - List list = new(); + List list = []; Assert.That(list.FirstOrGiven(), Is.InstanceOf()); } [Test] public void TestFirstOrGivenNoPredicate() { - List list = new() - { - new ListObject {Index = 0}, - new ListObject {Index = 1}, - new ListObject {Index = 2}, - new ListObject {Index = 3} - }; + List list = + [ + new ListObject { Index = 0 }, + new ListObject { Index = 1 }, + new ListObject { Index = 2 }, + new ListObject { Index = 3 } + ]; ListObject listObject = list.FirstOrGiven(); @@ -59,20 +59,20 @@ public class EnumerableExtensionTest [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")] public void TestFirstOrGivenPredicateEmpty() { - List list = new(); + List list = []; Assert.That(list.FirstOrGiven(o => o.Index == 2), Is.InstanceOf()); } [Test] public void TestFirstOrGivenPredicate() { - List list = new() - { - new ListObject {Index = 0}, - new ListObject {Index = 1}, - new ListObject {Index = 2}, - new ListObject {Index = 3} - }; + List list = + [ + new ListObject { Index = 0 }, + new ListObject { Index = 1 }, + new ListObject { Index = 2 }, + new ListObject { Index = 3 } + ]; ListObject listObject = list.FirstOrGiven(o => o.Index == 2);