From 98b467184b959bf5a0bda4dd828219945579c73f Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Tue, 1 Dec 2020 15:47:53 -0600 Subject: [PATCH] Remove unnecessary IsStatic checks in ActivatorUtilities --- .../src/ActivatorUtilities.cs | 45 +++++++------------ .../ActivatorUtilitiesTests.cs | 9 +++- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs index 25a7267924bc64..92be14984876c8 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs @@ -40,33 +40,30 @@ public static object CreateInstance( { foreach (ConstructorInfo? constructor in instanceType.GetConstructors()) { - if (!constructor.IsStatic) - { - var matcher = new ConstructorMatcher(constructor); - bool isPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false); - int length = matcher.Match(parameters); + var matcher = new ConstructorMatcher(constructor); + bool isPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false); + int length = matcher.Match(parameters); - if (isPreferred) + if (isPreferred) + { + if (seenPreferred) { - if (seenPreferred) - { - ThrowMultipleCtorsMarkedWithAttributeException(); - } - - if (length == -1) - { - ThrowMarkedCtorDoesNotTakeAllProvidedArguments(); - } + ThrowMultipleCtorsMarkedWithAttributeException(); } - if (isPreferred || bestLength < length) + if (length == -1) { - bestLength = length; - bestMatcher = matcher; + ThrowMarkedCtorDoesNotTakeAllProvidedArguments(); } + } - seenPreferred |= isPreferred; + if (isPreferred || bestLength < length) + { + bestLength = length; + bestMatcher = matcher; } + + seenPreferred |= isPreferred; } } @@ -232,11 +229,6 @@ private static bool TryFindMatchingConstructor( { foreach (ConstructorInfo? constructor in instanceType.GetConstructors()) { - if (constructor.IsStatic) - { - continue; - } - if (TryCreateParameterMap(constructor.GetParameters(), argumentTypes, out int?[] tempParameterMap)) { if (matchingConstructor != null) @@ -268,11 +260,6 @@ private static bool TryFindPreferredConstructor( bool seenPreferred = false; foreach (ConstructorInfo? constructor in instanceType.GetConstructors()) { - if (constructor.IsStatic) - { - continue; - } - if (constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false)) { if (seenPreferred) diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Specification.Tests/ActivatorUtilitiesTests.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Specification.Tests/ActivatorUtilitiesTests.cs index 8ef93a5179ba56..8bfd657ae0de0b 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Specification.Tests/ActivatorUtilitiesTests.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Specification.Tests/ActivatorUtilitiesTests.cs @@ -141,7 +141,7 @@ public void TypeActivatorCanDisambiguateConstructorsWithUniqueArguments(CreateIn public static IEnumerable TypesWithNonPublicConstructorData => CreateInstanceFuncs.Zip( - new[] { typeof(ClassWithPrivateCtor), typeof(ClassWithInternalConstructor), typeof(ClassWithProtectedConstructor) }, + new[] { typeof(ClassWithPrivateCtor), typeof(ClassWithInternalConstructor), typeof(ClassWithProtectedConstructor), typeof(StaticConstructorClass) }, (a, b) => new object[] { a[0], b }); [Theory] @@ -401,5 +401,12 @@ public Bar() throw new InvalidOperationException("some error"); } } + + class StaticConstructorClass + { + static StaticConstructorClass() { } + + private StaticConstructorClass() { } + } } }