@@ -124,6 +124,87 @@ void Test()
124124 }
125125 }
126126
127+ [ Fact ]
128+ public void CanBindIConfiguration ( )
129+ {
130+ var dic = new Dictionary < string , string >
131+ {
132+ { "Section:Integer" , "-2" } ,
133+ { "Section:Boolean" , "TRUe" } ,
134+ { "Section:Nested:Integer" , "11" } ,
135+ { "Section:Virtual" , "Sup" }
136+ } ;
137+ var configurationBuilder = new ConfigurationBuilder ( ) ;
138+ configurationBuilder . AddInMemoryCollection ( dic ) ;
139+ var config = configurationBuilder . Build ( ) ;
140+
141+ var options = config . Get < ConfigurationIConfigurationOptions > ( ) ;
142+ var childOptions = options . Section . Get < DerivedOptions > ( ) ;
143+ Test ( ) ;
144+
145+ options = ( ConfigurationIConfigurationOptions ) config . Get ( typeof ( ConfigurationIConfigurationOptions ) ) ;
146+ childOptions = ( DerivedOptions ) options . Section . Get ( typeof ( DerivedOptions ) ) ;
147+ Test ( ) ;
148+
149+ options = config . Get < ConfigurationIConfigurationOptions > ( options => { } ) ;
150+ childOptions = options . Section . Get < DerivedOptions > ( options => { } ) ;
151+ Test ( ) ;
152+
153+ options = ( ConfigurationIConfigurationOptions ) config . Get ( typeof ( ConfigurationIConfigurationOptions ) , options => { } ) ;
154+ childOptions = ( DerivedOptions ) options . Section . Get ( typeof ( DerivedOptions ) , options => { } ) ;
155+ Test ( ) ;
156+
157+ void Test ( )
158+ {
159+ Assert . True ( childOptions . Boolean ) ;
160+ Assert . Equal ( - 2 , childOptions . Integer ) ;
161+ Assert . Equal ( 11 , childOptions . Nested . Integer ) ;
162+ Assert . Equal ( "Derived:Sup" , childOptions . Virtual ) ;
163+
164+ var section = Assert . IsAssignableFrom < IConfigurationSection > ( options . Section ) ;
165+ Assert . Equal ( "Section" , section . Key ) ;
166+ Assert . Equal ( "Section" , section . Path ) ;
167+ Assert . Null ( section . Value ) ;
168+ }
169+ }
170+
171+ [ Fact ]
172+ public void CanBindIConfigurationWithDerivedOptionsSection ( )
173+ {
174+ var dic = new Dictionary < string , string >
175+ {
176+ { "Section:Integer" , "-2" } ,
177+ { "Section:Boolean" , "TRUe" } ,
178+ { "Section:Nested:Integer" , "11" } ,
179+ { "Section:Virtual" , "Sup" } ,
180+ { "Section:DerivedSection:Nested:Integer" , "11" } ,
181+ { "Section:DerivedSection:Virtual" , "Sup" }
182+ } ;
183+ var configurationBuilder = new ConfigurationBuilder ( ) ;
184+ configurationBuilder . AddInMemoryCollection ( dic ) ;
185+ var config = configurationBuilder . Build ( ) ;
186+
187+ var options = config . Get < ConfigurationIConfigurationOptions > ( ) ;
188+ var childOptions = options . Section . Get < DerivedOptionsWithIConfiguration > ( ) ;
189+ var childDerivedOptions = childOptions . DerivedSection . Get < DerivedOptions > ( ) ;
190+
191+ Assert . True ( childOptions . Boolean ) ;
192+ Assert . Equal ( - 2 , childOptions . Integer ) ;
193+ Assert . Equal ( 11 , childOptions . Nested . Integer ) ;
194+ Assert . Equal ( "Derived:Sup" , childOptions . Virtual ) ;
195+ Assert . Equal ( 11 , childDerivedOptions . Nested . Integer ) ;
196+ Assert . Equal ( "Derived:Sup" , childDerivedOptions . Virtual ) ;
197+
198+ var section = Assert . IsAssignableFrom < IConfigurationSection > ( options . Section ) ;
199+ Assert . Equal ( "Section" , section . Key ) ;
200+ Assert . Equal ( "Section" , section . Path ) ;
201+
202+ var derivedSection = Assert . IsAssignableFrom < IConfigurationSection > ( childOptions . DerivedSection ) ;
203+ Assert . Equal ( "DerivedSection" , derivedSection . Key ) ;
204+ Assert . Equal ( "Section:DerivedSection" , derivedSection . Path ) ;
205+ Assert . Null ( section . Value ) ;
206+ }
207+
127208 [ Fact ]
128209 public void CanBindWithKeyOverload ( )
129210 {
@@ -2649,6 +2730,44 @@ static void ValidateList(List<IConfigurationSection> list)
26492730 }
26502731 }
26512732
2733+ [ Fact ]
2734+ public void GetIConfiguration ( )
2735+ {
2736+ var configuration = TestHelpers . GetConfigurationFromJsonString ( """
2737+ {
2738+ "vaLue": "MyString",
2739+ }
2740+ """ ) ;
2741+
2742+ var obj = configuration . GetSection ( "value" ) . Get < IConfiguration > ( ) ;
2743+ var section = Assert . IsAssignableFrom < IConfigurationSection > ( obj ) ;
2744+ Assert . Equal ( "MyString" , section . Value ) ;
2745+
2746+ configuration = TestHelpers . GetConfigurationFromJsonString ( """
2747+ {
2748+ "vaLue": [ "MyString", { "nested": "value" } ],
2749+ }
2750+ """ ) ;
2751+
2752+ var list = configuration . GetSection ( "value" ) . Get < List < IConfiguration > > ( ) ;
2753+ ValidateList ( list ) ;
2754+
2755+ var dict = configuration . Get < Dictionary < string , List < IConfiguration > > > ( ) ;
2756+ Assert . Equal ( 1 , dict . Count ) ;
2757+ ValidateList ( dict [ "vaLue" ] ) ;
2758+
2759+ static void ValidateList ( List < IConfiguration > list )
2760+ {
2761+ Assert . Equal ( 2 , list . Count ) ;
2762+ Assert . Equal ( "0" , Assert . IsAssignableFrom < IConfigurationSection > ( list [ 0 ] ) . Key ) ;
2763+ Assert . Equal ( "MyString" , Assert . IsAssignableFrom < IConfigurationSection > ( list [ 0 ] ) . Value ) ;
2764+
2765+ Assert . Equal ( "1" , Assert . IsAssignableFrom < IConfigurationSection > ( list [ 1 ] ) . Key ) ;
2766+ var nestedSection = Assert . IsAssignableFrom < IConfigurationSection > ( list [ 1 ] . GetSection ( "nested" ) ) ;
2767+ Assert . Equal ( "value" , nestedSection . Value ) ;
2768+ }
2769+ }
2770+
26522771 [ Fact ]
26532772 public void NullableDictKeys ( )
26542773 {
0 commit comments