Direct Access

There are three methods of accessing configuration values directly via Configuration:

  • Dynamic Attributes generated from configuration data.

  • Configuration keys accessed by calling get() and set() methods.

  • Configuration keys accessed using dictionary-like indexer syntax.

All of the above Direct Access methods are equivalent and refer to the same underlying data (no copies are made.)

Dynamic Attributes

When configuration values are populated into a Configuration object, attributes are dynamically attached to the object based on configuration keys used in the configuration source. These dynamic attributes can be used to read/write configuration values:

config = ConfigurationBuilder()\
     .add_provider(JsonConfigurationProvider(json="""
     {
         "ConnectionStrings": {
             "SampleDb": "my_cxn_string"
         },
     }
     """))\
     .build()

 print(config.ConnectionStrings.SampleDb) # outputs: "my_cxn_string"

The get() and set() Methods

Configuration values can be accessed using their associated keys by calling get() and set() methods:

print(config.get('ConnectionStrings:SampleDb')) # outputs: "my_cxn_string"
print(config.get('ConnectionStrings__SampleDb')) # outputs: "my_cxn_string"
print(config.get('ConnectionStrings').get('SampleDb')) # outputs: "my_cxn_string"

Dictionary-like Interface

Configuration also exposes a dictionary-like interface providing keyed access to configuration values using indexer syntax:

print(config['ConnectionStrings:SampleDb']) # outputs: "my_cxn_string"
print(config['ConnectionStrings__SampleDb']) # outputs: "my_cxn_string"
print(config['ConnectionStrings']['SampleDb']) # outputs: "my_cxn_string"

In addition to the above indexer syntax, Configuration also supports additional dictionary-like methods such as items(), keys(), and values() (and others) – in most cases Configuration can be used as a stand-in where a dict would normally be used. However, type-checking will show that it is not a dict subclass. If you have some code that strictly requires a dict you can use the toDictionary() method to acquire an actual dictionary.