diff --git a/src/Autofac/Core/NamedPropertyParameter.cs b/src/Autofac/Core/NamedPropertyParameter.cs
index f94c74a7c..1723fad32 100644
--- a/src/Autofac/Core/NamedPropertyParameter.cs
+++ b/src/Autofac/Core/NamedPropertyParameter.cs
@@ -22,7 +22,7 @@ public class NamedPropertyParameter : ConstantParameter
///
/// The name of the property.
/// The property value.
- public NamedPropertyParameter(string name, object value)
+ public NamedPropertyParameter(string name, object? value)
: base(value, pi =>
{
return pi.TryGetDeclaringProperty(out PropertyInfo? prop) &&
diff --git a/src/Autofac/NamedParameter.cs b/src/Autofac/NamedParameter.cs
index 421301d1d..e8b38eeee 100644
--- a/src/Autofac/NamedParameter.cs
+++ b/src/Autofac/NamedParameter.cs
@@ -45,7 +45,7 @@ public class NamedParameter : ConstantParameter
///
/// The name of the parameter.
/// The parameter value.
- public NamedParameter(string name, object value)
+ public NamedParameter(string name, object? value)
: base(value, pi => pi.Name == name) =>
Name = Enforce.ArgumentNotNullOrEmpty(name, "name");
}
diff --git a/src/Autofac/PositionalParameter.cs b/src/Autofac/PositionalParameter.cs
index 92be04335..ce4db4c34 100644
--- a/src/Autofac/PositionalParameter.cs
+++ b/src/Autofac/PositionalParameter.cs
@@ -46,7 +46,7 @@ public class PositionalParameter : ConstantParameter
///
/// The zero-based position of the parameter.
/// The parameter value.
- public PositionalParameter(int position, object value)
+ public PositionalParameter(int position, object? value)
: base(value, pi => pi.Position == position && (pi.Member is ConstructorInfo))
{
if (position < 0)
diff --git a/src/Autofac/RegistrationExtensions.cs b/src/Autofac/RegistrationExtensions.cs
index 18a2c1c1b..515a8d352 100644
--- a/src/Autofac/RegistrationExtensions.cs
+++ b/src/Autofac/RegistrationExtensions.cs
@@ -621,7 +621,7 @@ public static IRegistrationBuilder
WithProperty(
this IRegistrationBuilder registration,
string propertyName,
- object propertyValue)
+ object? propertyValue)
where TReflectionActivatorData : ReflectionActivatorData
{
return registration.WithProperty(new NamedPropertyParameter(propertyName, propertyValue));
@@ -684,11 +684,6 @@ public static IRegistrationBuilder
throw new ArgumentNullException(nameof(propertyExpression));
}
- if (propertyValue == null)
- {
- throw new ArgumentNullException(nameof(propertyValue));
- }
-
var propertyInfo = (propertyExpression.Body as MemberExpression)?.Member as PropertyInfo ?? throw new ArgumentOutOfRangeException(nameof(propertyExpression), RegistrationExtensionsResources.ExpressionDoesNotReferToProperty);
return registration.WithProperty(new NamedPropertyParameter(propertyInfo.Name, propertyValue));
}
diff --git a/test/Autofac.Specification.Test/Features/PropertyInjectionTests.cs b/test/Autofac.Specification.Test/Features/PropertyInjectionTests.cs
index 0165a495f..b95b74bc8 100644
--- a/test/Autofac.Specification.Test/Features/PropertyInjectionTests.cs
+++ b/test/Autofac.Specification.Test/Features/PropertyInjectionTests.cs
@@ -398,6 +398,39 @@ public void DecoratedInstanceWithPropertyInjectionAllowingCircularReferencesStil
instance.AssertProp();
}
+ [Fact]
+ public void WithPropertyDelegateAllowsNullValue()
+ {
+ // Issue 1427: WithProperty should consistently allow null values.
+ var builder = new ContainerBuilder();
+ builder.RegisterType().WithProperty(t => t.Val, null);
+ var container = builder.Build();
+ var instance = container.Resolve();
+ Assert.Null(instance.Val);
+ }
+
+ [Fact]
+ public void WithPropertyNamedAllowsNullValue()
+ {
+ // Issue 1427: WithProperty should consistently allow null values.
+ var builder = new ContainerBuilder();
+ builder.RegisterType().WithProperty(nameof(HasPublicSetter.Val), null);
+ var container = builder.Build();
+ var instance = container.Resolve();
+ Assert.Null(instance.Val);
+ }
+
+ [Fact]
+ public void WithPropertyTypedAllowsNullValue()
+ {
+ // Issue 1427: WithProperty should consistently allow null values.
+ var builder = new ContainerBuilder();
+ builder.RegisterType().WithProperty(TypedParameter.From(null));
+ var container = builder.Build();
+ var instance = container.Resolve();
+ Assert.Null(instance.Val);
+ }
+
private class ConstructorParamNotAttachedToProperty
{
[SuppressMessage("SA1401", "SA1401")]