How to get the list of IOptions available from DI in c# Core?

前端 未结 2 817
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-15 22:19

Following this post: https://blog.bredvid.no/validating-configuration-in-asp-net-core-e9825bd15f10

I m now available to validate the Settings when a service need it.

2条回答
  •  不要未来只要你来
    2021-01-15 22:30

    You can get a list of configured option types by iterating the IServiceCollection instance:

    var configuredOptionTypes =
        from descriptor in services
        let serviceType = descriptor.ServiceType
        where serviceType.IsGenericType
        where serviceType.GetGenericTypeDefinition() == typeof(IConfigureNamedOptions<>)
        let optionType = serviceType.GetGenericArguments()[0]
        select optionType;
    

提交回复
热议问题