diff --git a/src/Extensions.Configuration.Object/Internal/ReflectionExtensions.cs b/src/Extensions.Configuration.Object/Internal/ReflectionExtensions.cs index de456ea..70349e1 100644 --- a/src/Extensions.Configuration.Object/Internal/ReflectionExtensions.cs +++ b/src/Extensions.Configuration.Object/Internal/ReflectionExtensions.cs @@ -45,6 +45,13 @@ public static PropertyInfo[] GetConfigurationProperties(this Type type) { var bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; var properties = type.GetProperties(bindingFlags); + + if (!type.IsAnonymous()) + { + return properties + .Where(x => !x.IsCompilerGenerated()) + .ToArray(); + } return properties; } diff --git a/test/Extensionsions.Configuration.Object.UnitTests/RecordObjectTest.cs b/test/Extensionsions.Configuration.Object.UnitTests/RecordObjectTest.cs new file mode 100644 index 0000000..3d681d3 --- /dev/null +++ b/test/Extensionsions.Configuration.Object.UnitTests/RecordObjectTest.cs @@ -0,0 +1,44 @@ +using FluentAssertions; +using Microsoft.Extensions.Configuration; +using Xunit; + +namespace Extensions.Configuration.Object.UnitTests +{ + public class RecordObjectTest + { + internal record MyConfiguration + { + public SubRecordConfiguration SubRecord; + public string MyKey; + } + + internal record SubRecordConfiguration + { + public string Title; + public string Name; + public int Age; + } + + [Fact] + public void AddObject_WithRecordObject_ShouldLoadFieldsIntoConfiguration() + { + var configuration = new ConfigurationBuilder() + .AddObject(new MyConfiguration + { + SubRecord = new SubRecordConfiguration + { + Title = "Editor", + Name = "Joe Smith", + Age = 33 + }, + MyKey = "My appsettings.json Value", + }) + .Build(); + + configuration["SubRecord:Title"].Should().Be("Editor"); + configuration["SubRecord:Name"].Should().Be("Joe Smith"); + configuration["SubRecord:Age"].Should().Be("33"); + configuration["MyKey"].Should().Be("My appsettings.json Value"); + } + } +}