Skip to content

Instantly share code, notes, and snippets.

@ezhevita
Last active November 4, 2025 21:23
Show Gist options
  • Select an option

  • Save ezhevita/9e526b42dfd01d9849a050d6ed5e67e9 to your computer and use it in GitHub Desktop.

Select an option

Save ezhevita/9e526b42dfd01d9849a050d6ed5e67e9 to your computer and use it in GitHub Desktop.
// <auto-generated/>
#nullable enable annotations
#nullable disable warnings
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0612, CS0618
namespace System.Runtime.CompilerServices
{
using System;
using System.CodeDom.Compiler;
[GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "9.0.12.47515")]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
file sealed class InterceptsLocationAttribute : Attribute
{
public InterceptsLocationAttribute(int version, string data)
{
}
}
}
namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
using Microsoft.Extensions.Configuration;
using System;
using System.CodeDom.Compiler;
using System.Globalization;
using System.Runtime.CompilerServices;
[GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "9.0.12.47515")]
file static class BindingExtensions
{
#region IConfiguration extensions.
/// <summary>Attempts to bind the configuration instance to a new instance of type T.</summary>
[InterceptsLocation(1, "LMBaO7nU/f09lZV5cJB4PogtAABBcmNoaUtlc3RyZWwuY3M=")] // /Users/ezhevita/Projects/ASF-upstream/ArchiSteamFarm/IPC/ArchiKestrel.cs(298,91)
public static T? Get<T>(this IConfiguration configuration) => (T?)(GetCore(configuration, typeof(T), configureOptions: null) ?? default(T));
#endregion IConfiguration extensions.
#region Core binding extensions.
public static object? GetCore(this IConfiguration configuration, Type type, Action<BinderOptions>? configureOptions)
{
ArgumentNullException.ThrowIfNull(configuration);
BinderOptions? binderOptions = GetBinderOptions(configureOptions);
if (!HasValueOrChildren(configuration))
{
return null;
}
if (type == typeof(global::System.Collections.Generic.HashSet<string>))
{
var instance = new global::System.Collections.Generic.HashSet<string>();
BindCore(configuration, ref instance, defaultValueIfNotFound: true, binderOptions);
return instance;
}
throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
}
public static void BindCore(IConfiguration configuration, ref global::System.Collections.Generic.HashSet<string> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
instance.Add(value);
}
}
}
/// <summary>If required by the binder options, validates that there are no unknown keys in the input configuration object.</summary>
public static void ValidateConfigurationKeys(Type type, Lazy<HashSet<string>> keys, IConfiguration configuration, BinderOptions? binderOptions)
{
if (binderOptions?.ErrorOnUnknownConfiguration is true)
{
List<string>? temp = null;
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (!keys.Value.Contains(section.Key))
{
(temp ??= new List<string>()).Add($"'{section.Key}'");
}
}
if (temp is not null)
{
throw new InvalidOperationException($"'ErrorOnUnknownConfiguration' was set on the provided BinderOptions, but the following properties were not found on the instance of {type}: {string.Join(", ", temp)}");
}
}
}
public static bool HasValueOrChildren(IConfiguration configuration)
{
if ((configuration as IConfigurationSection)?.Value is not null)
{
return true;
}
return AsConfigWithChildren(configuration) is not null;
}
public static IConfiguration? AsConfigWithChildren(IConfiguration configuration)
{
foreach (IConfigurationSection _ in configuration.GetChildren())
{
return configuration;
}
return null;
}
public static BinderOptions? GetBinderOptions(Action<BinderOptions>? configureOptions)
{
if (configureOptions is null)
{
return null;
}
BinderOptions binderOptions = new();
configureOptions(binderOptions);
if (binderOptions.BindNonPublicProperties)
{
throw new NotSupportedException($"The configuration binding source generator does not support 'BinderOptions.BindNonPublicProperties'.");
}
return binderOptions;
}
#endregion Core binding extensions.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment