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

Relax constructor type requirements #47661

Conversation

GabeDeBacker
Copy link

This change allows deserialization into object where it contains a member property (or field) whose type is "assignable" instead to the constructor type. This address some of the ideas mentioned in #44428.

Previously a class a simple as this would not "round trip" through System.Text.Json (which is a common pattern for applications using data binding to WPF/XAML, etc.)

public class ClassWithConstructorArgumentTypeThatIsAssignableFromImmutablePropertyType
{
   public ReadOnlyObservableCollection<int> Values { get; }
   private readonly ObservableCollection<int> valueCollection;
 
   public ClassWithConstructorArgumentTypeThatIsAssignableFromImmutablePropertyType(IEnumerable<int> values)
    {
        this.valueCollection = new ObservableCollection<int>(values);
        this.Values = new ReadOnlyObservableCollection<int>(this.valueCollection);
    }
}

…trctor will be called if the member property or field type is assignable to the constructor parameter type. For example, this allows a constructor parameter of IEnumerable<T> x to be called when the property type is ReadOnlyObservableCollection<T>
@ghost
Copy link

ghost commented Jan 29, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

This change allows deserialization into object where it contains a member property (or field) whose type is "assignable" instead to the constructor type. This address some of the ideas mentioned in #44428.

Previously a class a simple as this would not "round trip" through System.Text.Json (which is a common pattern for applications using data binding to WPF/XAML, etc.)

public class ClassWithConstructorArgumentTypeThatIsAssignableFromImmutablePropertyType
{
   public ReadOnlyObservableCollection<int> Values { get; }
   private readonly ObservableCollection<int> valueCollection;
 
   public ClassWithConstructorArgumentTypeThatIsAssignableFromImmutablePropertyType(IEnumerable<int> values)
    {
        this.valueCollection = new ObservableCollection<int>(values);
        this.Values = new ReadOnlyObservableCollection<int>(this.valueCollection);
    }
}
Author: GabeDeBacker
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@dnfadmin
Copy link

dnfadmin commented Jan 29, 2021

CLA assistant check
All CLA requirements met.

@layomia layomia self-assigned this Feb 1, 2021
Base automatically changed from master to main March 1, 2021 09:07
@ericstj
Copy link
Member

ericstj commented Mar 8, 2021

@eiriktsarpalis @layomia can you have a look?

@ericstj
Copy link
Member

ericstj commented May 26, 2021

Ping. Reviewers, please have a look and provide next steps (other than rebase of course).

@layomia
Copy link
Contributor

layomia commented Jun 8, 2021

I chatted with @GabeDeBacker offline - I'll pick up the work here. Thanks for the effort!

@layomia layomia closed this Jun 8, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jul 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants