IOptionsSnapshot get all registered names

Viewed 71

Using Microsoft.Extensions.Options.IOptionsSnapshot you can configure one config class multiple times.

Imagine I have:

{
    "MyObject": {
        "AnotherObject": {
            "Address": { "City": "..." } // Will be named as "MyObject:AnotherObject:Address" in the snapshot
        }
    },
    "AnotherSetting": {
        "Address": { "City": "..." } 
            // This object will be registered with a Guid.ToString() 
            // (I cannot change the package registering it)
    }
    "FavoriteAddresses" {
        "Home": { "City": "..." },
        "Work": { "City": "..." },
        "Parents": { "City": "..." },
        // ...... Unlimited list
    }
}

I have to deal with the following restrictions:

  • I cannot change my Address class to add a Name property and transform FavoriteAddresses to an array
  • Neither I can apply inheritance or have wrapper around this Address class

As you can see, many Address objects and for some it is impossible to know the chosen name. Regardless where it is registered, it all ends up as a named option.

So how can I get a list of all registred names for the Address options?

0 Answers
Related