diff --git a/src/DryIoc/Container.cs b/src/DryIoc/Container.cs index cbedbe8b8..96542ac66 100644 --- a/src/DryIoc/Container.cs +++ b/src/DryIoc/Container.cs @@ -39,7 +39,7 @@ namespace DryIoc using System.Collections; using System.Collections.Generic; using System.Diagnostics; // for StackTrace - using System.Diagnostics.CodeAnalysis; // for SuppressMessage + using System.Diagnostics.CodeAnalysis; // for SuppressMessage, SetsRequiredPropertiesAttribute using System.Linq; using System.Reflection; using System.Reflection.Emit; diff --git a/test/DryIoc.UnitTests/RequiredPropertiesTests.cs b/test/DryIoc.UnitTests/RequiredPropertiesTests.cs index c4dfa5c14..667acc034 100644 --- a/test/DryIoc.UnitTests/RequiredPropertiesTests.cs +++ b/test/DryIoc.UnitTests/RequiredPropertiesTests.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; namespace DryIoc.UnitTests { @@ -12,7 +13,8 @@ public int Run() { Can_inject_required_properties(); Should_throw_for_unresolved_required_property(); - return 2; + Should_skip_required_property_injection_when_using_ctor_with_SetsRequiredProperties(); + return 3; } [Test] @@ -46,6 +48,22 @@ public void Should_throw_for_unresolved_required_property() Assert.AreEqual(Error.NameOf(Error.UnableToResolveUnknownService), ex.ErrorName); } + [Test] + public void Should_skip_required_property_injection_when_using_ctor_with_SetsRequiredProperties() + { + var c = new Container(Rules.Default.With(propertiesAndFields: PropertiesAndFields.RequiredProperties())); + + c.Register(); + + c.Register(); + c.Register(); + + var x = c.Resolve(); + + Assert.Null(x.A); + Assert.NotNull(x.B); + } + public class A {} public class B {} public class C {} @@ -63,6 +81,15 @@ public class BS { public required D D { protected get; set; } } + + public class SS + { + public required A A { get; init; } + public B B { get; private set; } + + [SetsRequiredMembers] + public SS(B b) => B = b; + } #endif } } \ No newline at end of file