Add Initialize Objects by Using an Object Initializer
Closed this issue · 0 comments
forcewake commented
We can use object initializers to initialize type objects in a declarative manner without explicitly invoking a constructor for the type.
In C# we want to have such code:
var entity = new Entity()
{
Name = "A",
Value = 12
};
In Roslyn it will looks like:
LocalDeclarationStatement(
VariableDeclaration(
IdentifierName("var"))
.WithVariables(
SingletonSeparatedList<VariableDeclaratorSyntax>(
VariableDeclarator(
Identifier("entity"))
.WithInitializer(
EqualsValueClause(
ObjectCreationExpression(
IdentifierName("Entity"))
.WithArgumentList(
ArgumentList())
.WithInitializer(
InitializerExpression(
SyntaxKind.ObjectInitializerExpression,
SeparatedList<ExpressionSyntax>(
new SyntaxNodeOrToken[]{
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("Name"),
LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal("A"))),
Token(SyntaxKind.CommaToken),
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("Value"),
LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(12)))}))))))))
.NormalizeWhitespace()
So I will do a PR in order to add this feature to this library.
Are you OK with it?