Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private static MethodInfo GetMethodInfo<T>(Expression<T> expr)
return service;
}

private static NewExpression BuildFactoryExpression(
private static BlockExpression BuildFactoryExpression(
ConstructorInfo constructor,
int?[] parameterMap,
Expression serviceProvider,
Expand Down Expand Up @@ -281,7 +281,8 @@ private static NewExpression BuildFactoryExpression(
constructorArguments[i] = Expression.Convert(constructorArguments[i], parameterType);
}

return Expression.New(constructor, constructorArguments);
return Expression.Block(Expression.IfThen(Expression.Equal(serviceProvider, Expression.Constant(null)), Expression.Throw(Expression.Constant(new ArgumentNullException(nameof(serviceProvider))))),
Expression.New(constructor, constructorArguments));
}

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
using static Microsoft.Extensions.DependencyInjection.Tests.AsyncServiceScopeTests;

namespace Microsoft.Extensions.DependencyInjection.Tests
{
Expand Down Expand Up @@ -91,6 +90,13 @@ public void TypeActivatorThrowsOnNullProvider()
Assert.Throws<ArgumentNullException>(() => ActivatorUtilities.CreateInstance<ClassWithABCS>(null, "hello"));
}

[Fact]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test

public void FactoryActivatorThrowsOnNullProvider()
{
var f = ActivatorUtilities.CreateFactory(typeof(ClassWithA), new Type[0]);
Assert.Throws<ArgumentNullException>(() => f(serviceProvider: null, null));
}

[Fact]
public void CreateInstance_ClassWithABCS_UsesTheLongestAvailableConstructor_ParameterOrderDoesntMatter()
{
Expand Down