Skip to content

Generate type should take into account the initializer to initialize fields/props as well. #58495

@CyrusNajmabadi

Description

@CyrusNajmabadi

Extracted from: #58491

class Program {
    void Main()
    {
        Test x = new() { A = 1, B = 1 };
    }
}

The genrating code for Test will be:

internal class Test
{
}

ignoring the initialization part. Unlike for this code:

class Program {
    void Main()
    {
        var x = new Test() { A = 1, B = 1 };
    }
}

the generated class is:


internal class Test
{
    public Test()
    {
    }

    public int A { get; set; }
    public int B { get; set; }
}

In fact, the two cases co-exist:

Test x = new Test() { A = 1, B = 1 };

Generating the class from Test x will not generate the fields, unlike generating it from new Test().
I think the generator should look for an initializer at the right hand side.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Completed

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions