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

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

Merged
merged 1 commit into from
Jan 10, 2022
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,17 +248,19 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig
return;
}

object? propertyValue = property.GetValue(instance);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what will happen if we just call GetPropertyValue(property, instance, config, options); here instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think more work needs to be done to investigate the correct fix that addresses #52905.

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

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

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

if (propertyValue != null)
if (propertyValue != null && hasSetter)
{
property.SetValue(instance, propertyValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ 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 @@ -348,20 +343,6 @@ 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