Skip to content

Commit da3e434

Browse files
authored
Remove unnecessary IsStatic checks in ActivatorUtilities (#28292)
1 parent fc70b25 commit da3e434

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

src/Shared/ActivatorUtilities/ActivatorUtilities.cs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,30 @@ public static object CreateInstance(IServiceProvider provider, Type instanceType
5151
{
5252
foreach (var constructor in instanceType.GetConstructors())
5353
{
54-
if (!constructor.IsStatic)
55-
{
56-
var matcher = new ConstructorMatcher(constructor);
57-
var isPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false);
58-
var length = matcher.Match(parameters);
54+
var matcher = new ConstructorMatcher(constructor);
55+
var isPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false);
56+
var length = matcher.Match(parameters);
5957

60-
if (isPreferred)
58+
if (isPreferred)
59+
{
60+
if (seenPreferred)
6161
{
62-
if (seenPreferred)
63-
{
64-
ThrowMultipleCtorsMarkedWithAttributeException();
65-
}
66-
67-
if (length == -1)
68-
{
69-
ThrowMarkedCtorDoesNotTakeAllProvidedArguments();
70-
}
62+
ThrowMultipleCtorsMarkedWithAttributeException();
7163
}
7264

73-
if (isPreferred || bestLength < length)
65+
if (length == -1)
7466
{
75-
bestLength = length;
76-
bestMatcher = matcher;
67+
ThrowMarkedCtorDoesNotTakeAllProvidedArguments();
7768
}
69+
}
7870

79-
seenPreferred |= isPreferred;
71+
if (isPreferred || bestLength < length)
72+
{
73+
bestLength = length;
74+
bestMatcher = matcher;
8075
}
76+
77+
seenPreferred |= isPreferred;
8178
}
8279
}
8380

@@ -237,11 +234,6 @@ private static bool TryFindMatchingConstructor(
237234
{
238235
foreach (var constructor in instanceType.GetConstructors())
239236
{
240-
if (constructor.IsStatic)
241-
{
242-
continue;
243-
}
244-
245237
if (TryCreateParameterMap(constructor.GetParameters(), argumentTypes, out int?[] tempParameterMap))
246238
{
247239
if (matchingConstructor != null)
@@ -267,11 +259,6 @@ private static bool TryFindPreferredConstructor(
267259
var seenPreferred = false;
268260
foreach (var constructor in instanceType.GetConstructors())
269261
{
270-
if (constructor.IsStatic)
271-
{
272-
continue;
273-
}
274-
275262
if (constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false))
276263
{
277264
if (seenPreferred)

0 commit comments

Comments
 (0)