Skip to content

Commit

Permalink
Remove unnecessary IsStatic checks in ActivatorUtilities (dotnet#28292)
Browse files Browse the repository at this point in the history
  • Loading branch information
eerhardt authored Dec 2, 2020
1 parent fc70b25 commit da3e434
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions src/Shared/ActivatorUtilities/ActivatorUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,30 @@ public static object CreateInstance(IServiceProvider provider, Type instanceType
{
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;
}
}

Expand Down Expand Up @@ -237,11 +234,6 @@ private static bool TryFindMatchingConstructor(
{
foreach (var constructor in instanceType.GetConstructors())
{
if (constructor.IsStatic)
{
continue;
}

if (TryCreateParameterMap(constructor.GetParameters(), argumentTypes, out int?[] tempParameterMap))
{
if (matchingConstructor != null)
Expand All @@ -267,11 +259,6 @@ private static bool TryFindPreferredConstructor(
var seenPreferred = false;
foreach (var constructor in instanceType.GetConstructors())
{
if (constructor.IsStatic)
{
continue;
}

if (constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false))
{
if (seenPreferred)
Expand Down

0 comments on commit da3e434

Please sign in to comment.