Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extra call to PropertyInfo.GetValue(object) #60415

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,17 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig
return;
}

object propertyValue = property.GetValue(instance);
bool hasSetter = property.SetMethod != null && (property.SetMethod.IsPublic || options.BindNonPublicProperties);

if (propertyValue == null && !hasSetter)
if (!hasSetter)
{
// Property doesn't have a value and we cannot set it so there is no
// point in going further down the graph
// The property cannot be set so there is no point going further
return;
}

propertyValue = GetPropertyValue(property, instance, config, options);
object propertyValue = GetPropertyValue(property, instance, config, options);

if (propertyValue != null && hasSetter)
if (propertyValue != null)
{
property.SetValue(instance, propertyValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public class ByteArrayOptions
public byte[] MyByteArray { get; set; }
}

public class GetterOnlyOptions
{
public string MyString => throw new NotImplementedException();
}

[Fact]
public void CanBindIConfigurationSection()
{
Expand Down Expand Up @@ -343,6 +348,20 @@ public void ThrowsIfPropertyInConfigMissingInNestedModel()
Assert.Equal(expectedMessage, ex.Message);
}

[Fact]
public void DoesNotExecuteGetterIfNoSetter()
{
var dic = new Dictionary<string, string>
{
{"MyString", "hello world"}
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();

var _ = config.Get<GetterOnlyOptions>();
}

[Fact]
public void GetDefaultsWhenDataDoesNotExist()
{
Expand Down