Class ConfigurationOptionsExtensions
- Namespace
- Acuit.Pinpoint.Configuration
- Assembly
- Acuit.Pinpoint.Configuration.dll
Extension methods for working with strongly-typed configuration options.
public static class ConfigurationOptionsExtensions
- Inheritance
-
ConfigurationOptionsExtensions
- Inherited Members
Methods
GetSectionFromSelector<TOptions, TMember>(IConfigurationSection, Expression<Func<TOptions, TMember>>)
Gets the configuration section that corresponds to a member of a strongly-typed options object.
public static IConfigurationSection GetSectionFromSelector<TOptions, TMember>(this IConfigurationSection configurationSection, Expression<Func<TOptions, TMember>> memberSelector)
Parameters
configurationSection
IConfigurationSectionThe configuration section that corresponds to the strongly-typed options object.
memberSelector
Expression<Func<TOptions, TMember>>A lambda expression that selects a member of the options object.
Returns
- IConfigurationSection
The configuration section corresponding to the member.
Type Parameters
TOptions
The strongly-typed options object type.
TMember
The member type.
Remarks
Only simple property or field member accesses and indexers with a single constant int argument are supported. For example:
var section = optionsSection.GetSectionFromSelector((TestOptions o) => o.Items[1].Name);
Lambda expressions with captured variables will not work. For example, the following will throw an InvalidOperationException:
int index = 1;
var section = optionsSection.GetSectionFromSelector((TestOptions o) => o.Items[index].Name);
// throws InvalidOperationException
The workaround for this is to use this pattern:
int index = 1;
var section = optionsSection
.GetSectionFromSelector((TestOptions o) => o.Items)
.GetChildren().ElementAt(index)
.GetSectionFromSelector((TestItem i) => i.Name);
Exceptions
- ArgumentNullException
configurationSection
is null.- ArgumentNullException
memberSelector
is null.- InvalidOperationException
The
memberSelector
lambda expression does not select a member via simple member accesses and/or constant int indexers.