Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize an object with Roslyn

I've really tried to solve this on my own, and it seems so simple but hopefully with the right pointer I can work out the rest of what I need, but...

How can I generate the following code with Rosyln:

var entity = new Entity
{
    Name = "A",
    Value = 12
};

I know SyntaxFactory.InitializerExpression is involved somewhere, but I'm just lost!

like image 962
joshcomley Avatar asked Jan 10 '23 16:01

joshcomley


1 Answers

The Roslyn Quoter tool is very handy for things like this:

roslynquoter.azurewebsites.net.

Now that it assumes you have an entire valid file

like image 70
Kevin Pilch Avatar answered Jan 20 '23 16:01

Kevin Pilch