Skip to content

Commit

Permalink
Fix for issue dotnet#54500: Refactored the TryGetServiceKey method an…
Browse files Browse the repository at this point in the history
…d added the NotNullWhen(true) attribute. Also changed the Inherit param from true to false when obtaining the custom attribute FromKeyedServicesAttribute.
  • Loading branch information
NicoBrabers committed May 17, 2024
1 parent 393802a commit 2bc5913
Showing 1 changed file with 5 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ public RequestDelegate CreateMiddleware(RequestDelegate next)
var precomputedKeys = new object?[parameters.Length];
for (var i = 1; i < parameters.Length; i++)
{
var hasServiceKey = TryGetServiceKey(parameters[i], out object? key);
_ = TryGetServiceKey(parameters[i], out object? key);

precomputedKeys[i] = key;
}

Expand All @@ -235,24 +236,11 @@ public RequestDelegate CreateMiddleware(RequestDelegate next)
};
}

private static bool TryGetServiceKey(ParameterInfo parameterInfo, out object? key)
private static bool TryGetServiceKey(ParameterInfo parameterInfo, [NotNullWhen(true)] out object? key)
{
if (parameterInfo.CustomAttributes != null)
{
foreach (var attribute in parameterInfo.GetCustomAttributes(true))
{
if (attribute is FromKeyedServicesAttribute keyed)
{
key = keyed.Key;

return true;
}
}
}

key = null;
key = parameterInfo.GetCustomAttribute<FromKeyedServicesAttribute>(false)?.Key;

return false;
return key != null;
}

private static UnaryExpression GetMethodArgument(ParameterInfo parameter, ParameterExpression providerArg, Type parameterType, Type? declaringType)
Expand Down

0 comments on commit 2bc5913

Please sign in to comment.