Skip to content

Commit

Permalink
fix: #5 Ignore properties with the CompilerGenerated attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
runemoennike authored Dec 5, 2023
1 parent 41fae51 commit 9719ba4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
}

0 comments on commit 9719ba4

Please sign in to comment.