Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert non-nullable changes
  • Loading branch information
maxkoshevoi committed Nov 12, 2021
commit 43e1e134af7da27c4b5b10321f0c283860bece2d
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class ConfigurationBinder
/// <returns>The new instance of T if successful, default(T) otherwise.</returns>
[RequiresUnreferencedCode(TrimmingWarningMessage)]
public static T? Get<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this IConfiguration configuration)
=> configuration.Get<T>(null);
=> configuration.Get<T>(_ => { });

/// <summary>
/// Attempts to bind the configuration instance to a new instance of type T.
Expand Down Expand Up @@ -67,7 +67,7 @@ public static class ConfigurationBinder
/// <returns>The new instance if successful, null otherwise.</returns>
[RequiresUnreferencedCode(TrimmingWarningMessage)]
public static object? Get(this IConfiguration configuration, Type type)
=> configuration.Get(type, null);
=> configuration.Get(type, _ => { });

/// <summary>
/// Attempts to bind the configuration instance to a new instance of type T.
Expand Down Expand Up @@ -207,36 +207,34 @@ public static void Bind(this IConfiguration configuration, object? instance, Act
[RequiresUnreferencedCode(PropertyTrimmingWarningMessage)]
private static void BindNonScalar(this IConfiguration configuration, object? instance, BinderOptions options)
{
if (instance == null)
if (instance != null)
{
return;
}
List<PropertyInfo> modelProperties = GetAllProperties(instance.GetType());

List<PropertyInfo> modelProperties = GetAllProperties(instance.GetType());
if (options.ErrorOnUnknownConfiguration)
{
HashSet<string> propertyNames = new(modelProperties.Select(mp => mp.Name),
StringComparer.OrdinalIgnoreCase);

if (options.ErrorOnUnknownConfiguration)
{
HashSet<string> propertyNames = new(modelProperties.Select(mp => mp.Name),
StringComparer.OrdinalIgnoreCase);
IEnumerable<IConfigurationSection> configurationSections = configuration.GetChildren();
List<string> missingPropertyNames = configurationSections
.Where(cs => !propertyNames.Contains(cs.Key))
.Select(mp => $"'{mp.Key}'")
.ToList();

IEnumerable<IConfigurationSection> configurationSections = configuration.GetChildren();
List<string> missingPropertyNames = configurationSections
.Where(cs => !propertyNames.Contains(cs.Key))
.Select(mp => $"'{mp.Key}'")
.ToList();
if (missingPropertyNames.Count > 0)
{
throw new InvalidOperationException(SR.Format(SR.Error_MissingConfig,
nameof(options.ErrorOnUnknownConfiguration), nameof(BinderOptions), instance.GetType(),
string.Join(", ", missingPropertyNames)));
}
}

if (missingPropertyNames.Count > 0)
foreach (PropertyInfo property in modelProperties)
{
throw new InvalidOperationException(SR.Format(SR.Error_MissingConfig,
nameof(options.ErrorOnUnknownConfiguration), nameof(BinderOptions), instance.GetType(),
string.Join(", ", missingPropertyNames)));
BindProperty(property, instance, configuration, options);
}
}

foreach (PropertyInfo property in modelProperties)
{
BindProperty(property, instance, configuration, options);
}
}

[RequiresUnreferencedCode(PropertyTrimmingWarningMessage)]
Expand Down Expand Up @@ -383,7 +381,7 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig
}
else if (type.IsArray)
{
instance = BindArray((Array)instance, config, options);
instance = BindArray((Array)instance!, config, options);
}
else
{
Expand All @@ -404,7 +402,7 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig
return instance;
}

private static object CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type)
private static object? CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type)
{
if (type.IsInterface || type.IsAbstract)
{
Expand All @@ -430,17 +428,14 @@ private static object CreateInstance([DynamicallyAccessedMembers(DynamicallyAcce
}
}

object? instance;
try
{
instance = Activator.CreateInstance(type);
return Activator.CreateInstance(type);
}
catch (Exception ex)
{
throw new InvalidOperationException(SR.Format(SR.Error_FailedToActivate, type), ex);
}

return instance ?? throw new InvalidOperationException(SR.Format(SR.Error_FailedToActivate, type));
}

[RequiresUnreferencedCode("Cannot statically analyze what the element type is of the value objects in the dictionary so its members may be trimmed.")]
Expand Down