Просмотр исходного кода

Remove unnecessary IsStatic checks in ActivatorUtilities (#28292)

Eric Erhardt 5 лет назад
Родитель
Сommit
da3e434e33
1 измененных файлов с 16 добавлено и 29 удалено
  1. 16 29
      src/Shared/ActivatorUtilities/ActivatorUtilities.cs

+ 16 - 29
src/Shared/ActivatorUtilities/ActivatorUtilities.cs

@@ -51,33 +51,30 @@ namespace Microsoft.Extensions.Internal
             {
                 foreach (var constructor in instanceType.GetConstructors())
                 {
-                    if (!constructor.IsStatic)
-                    {
-                        var matcher = new ConstructorMatcher(constructor);
-                        var isPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false);
-                        var length = matcher.Match(parameters);
+                    var matcher = new ConstructorMatcher(constructor);
+                    var isPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false);
+                    var 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;
                 }
             }
 
@@ -237,11 +234,6 @@ namespace Microsoft.Extensions.Internal
         {
             foreach (var constructor in instanceType.GetConstructors())
             {
-                if (constructor.IsStatic)
-                {
-                    continue;
-                }
-
                 if (TryCreateParameterMap(constructor.GetParameters(), argumentTypes, out int?[] tempParameterMap))
                 {
                     if (matchingConstructor != null)
@@ -267,11 +259,6 @@ namespace Microsoft.Extensions.Internal
             var seenPreferred = false;
             foreach (var constructor in instanceType.GetConstructors())
             {
-                if (constructor.IsStatic)
-                {
-                    continue;
-                }
-
                 if (constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false))
                 {
                     if (seenPreferred)