Skip to content

Commit b288578

Browse files
committed
added Should_skip_required_property_injection_when_using_ctor_with_SetsRequiredProperties for #563
1 parent 7e0f63f commit b288578

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/DryIoc/Container.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace DryIoc
3939
using System.Collections;
4040
using System.Collections.Generic;
4141
using System.Diagnostics; // for StackTrace
42-
using System.Diagnostics.CodeAnalysis; // for SuppressMessage
42+
using System.Diagnostics.CodeAnalysis; // for SuppressMessage, SetsRequiredPropertiesAttribute
4343
using System.Linq;
4444
using System.Reflection;
4545
using System.Reflection.Emit;

test/DryIoc.UnitTests/RequiredPropertiesTests.cs

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using System.Diagnostics.CodeAnalysis;
2+
using NUnit.Framework;
23

34
namespace DryIoc.UnitTests
45
{
@@ -12,7 +13,8 @@ public int Run()
1213
{
1314
Can_inject_required_properties();
1415
Should_throw_for_unresolved_required_property();
15-
return 2;
16+
Should_skip_required_property_injection_when_using_ctor_with_SetsRequiredProperties();
17+
return 3;
1618
}
1719

1820
[Test]
@@ -46,6 +48,22 @@ public void Should_throw_for_unresolved_required_property()
4648
Assert.AreEqual(Error.NameOf(Error.UnableToResolveUnknownService), ex.ErrorName);
4749
}
4850

51+
[Test]
52+
public void Should_skip_required_property_injection_when_using_ctor_with_SetsRequiredProperties()
53+
{
54+
var c = new Container(Rules.Default.With(propertiesAndFields: PropertiesAndFields.RequiredProperties()));
55+
56+
c.Register<SS>();
57+
58+
c.Register<A>();
59+
c.Register<B>();
60+
61+
var x = c.Resolve<SS>();
62+
63+
Assert.Null(x.A);
64+
Assert.NotNull(x.B);
65+
}
66+
4967
public class A {}
5068
public class B {}
5169
public class C {}
@@ -63,6 +81,15 @@ public class BS
6381
{
6482
public required D D { protected get; set; }
6583
}
84+
85+
public class SS
86+
{
87+
public required A A { get; init; }
88+
public B B { get; private set; }
89+
90+
[SetsRequiredMembers]
91+
public SS(B b) => B = b;
92+
}
6693
#endif
6794
}
6895
}

0 commit comments

Comments
 (0)