File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -45,7 +45,8 @@ public EnvBinder(IEnvironmentVariablesProvider provider)
4545 var settings = new TSettings ( ) ;
4646 var type = typeof ( TSettings ) ;
4747 result = _validationResult ;
48- foreach ( PropertyInfo property in type . GetProperties ( ) )
48+ var properties = _configuration . BindNonPublicProperties ? type . GetPublicAndNonPublicProperties ( ) : type . GetProperties ( ) ;
49+ foreach ( PropertyInfo property in properties )
4950 {
5051 if ( IsReadOnlyOrWriteOnly ( property ) )
5152 continue ;
@@ -84,12 +85,19 @@ public EnvBinder(IEnvironmentVariablesProvider provider)
8485 /// <returns><c>true</c> if the property is read-only or write-only, or <c>false</c> if the property is read-write.</returns>
8586 private bool IsReadOnlyOrWriteOnly ( PropertyInfo property )
8687 => ! property . CanRead || ! property . CanWrite ;
87-
88+
8889 /// <inheritdoc />
8990 public IEnvBinder IgnoreException ( )
9091 {
9192 _configuration . ThrowException = false ;
9293 return this ;
9394 }
95+
96+ /// <inheritdoc />
97+ public IEnvBinder AllowBindNonPublicProperties ( )
98+ {
99+ _configuration . BindNonPublicProperties = true ;
100+ return this ;
101+ }
94102 }
95103}
Original file line number Diff line number Diff line change @@ -13,6 +13,12 @@ internal class EnvBinderOptions
1313 /// </summary>
1414 public bool ThrowException { get ; set ; } = true ;
1515
16+ /// <summary>
17+ /// When <c>false</c> (the default), the binder will only to set public properties.
18+ /// If <c>true</c>, the binder will to set all non-public properties.
19+ /// </summary>
20+ public bool BindNonPublicProperties { get ; set ; }
21+
1622 /// <summary>
1723 /// Gets or sets the environment variables provider.
1824 /// </summary>
Original file line number Diff line number Diff line change @@ -13,12 +13,13 @@ public interface IEnvBinder
1313 TSettings Bind < TSettings > ( out EnvValidationResult result ) where TSettings : new ( ) ;
1414
1515 /// <summary>
16- /// Binds the instance of the environment variables provider to a new instance of type TSettings (binds only read-write properties) .
16+ /// Binds the instance of the environment variables provider to a new instance of type TSettings.
1717 /// </summary>
1818 /// <typeparam name="TSettings">The type of the new instance to bind.</typeparam>
1919 /// <exception cref="BinderException">
2020 /// If the binder encounters one or more errors.
2121 /// </exception>
22+ /// <remarks>Binds only read-write properties and, moreover, only sets public properties.</remarks>
2223 /// <returns>The new instance of TSettings.</returns>
2324 TSettings Bind < TSettings > ( ) where TSettings : new ( ) ;
2425
@@ -27,5 +28,12 @@ public interface IEnvBinder
2728 /// </summary>
2829 /// <returns>An instance implementing the fluent interface.</returns>
2930 IEnvBinder IgnoreException ( ) ;
31+
32+ /// <summary>
33+ /// Allows binding of non-public properties.
34+ /// This method tells the binder that it can set non-public properties.
35+ /// </summary>
36+ /// <returns>An instance implementing the fluent interface.</returns>
37+ IEnvBinder AllowBindNonPublicProperties ( ) ;
3038 }
3139}
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Reflection ;
4+
5+ namespace DotEnv . Core
6+ {
7+ internal static class TypeExtensions
8+ {
9+ /// <summary>
10+ /// Returns all public and non-public properties.
11+ /// </summary>
12+ /// <param name="type"></param>
13+ /// <returns>
14+ /// An array of <see cref="PropertyInfo" /> objects representing all public and non-public properties of the current <see cref="Type" />.
15+ /// </returns>
16+ public static PropertyInfo [ ] GetPublicAndNonPublicProperties ( this Type type )
17+ => type . GetProperties ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) ;
18+ }
19+ }
You can’t perform that action at this time.
0 commit comments