Skip to content

ListInitExpression from Expression list fails with multiple expression types. #16317

Open
@JonHanna

Description

Consider:

private class MixedAddable : IEnumerable
{
    private readonly List<string> _strings = new List<string>();
    private readonly List<int> _ints = new List<int>();

    public void Add(string value)
    {
        _strings.Add(value);
    }

    public void Add(int value)
    {
        _ints.Add(value);
    }

    public IEnumerator GetEnumerator()
    {
        return _strings.Concat(_ints.Select(i => i.ToString())).GetEnumerator();
    }
}

The likes of () => new MixedAddable { 1, "a", 2, "b" } works fine, but the following fails:

Expression.ListInit(
    Expression.New(typeof(MixedAddable)),
    Expression.Constant(0),
    Expression.Constant("a")
)

It's assumed that the correct Add method for the first expression is also correct for the rest, though this doesn't necessarily hold.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    area-System.Linq.Expressionsdesign-discussionOngoing discussion about design without consensusenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions