Console.WriteLine("Hello World!");
var consoleWriteLine = SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("Console"), SyntaxFactory.IdentifierName("WriteLine")); var invocation = SyntaxFactory.InvocationExpression( consoleWriteLine, SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList( SyntaxFactory.Argument( SyntaxFactory.LiteralExpression( SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal("Hello World!"))))));
var obj = new MyClass();
var objCreation = SyntaxFactory.ObjectCreationExpression( SyntaxFactory.IdentifierName("MyClass")) .WithArgumentList(SyntaxFactory.ArgumentList()); var decl = SyntaxFactory.LocalDeclarationStatement( SyntaxFactory.VariableDeclaration( SyntaxFactory.IdentifierName("var")) .WithVariables( SyntaxFactory.SingletonSeparatedList( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier("obj")) .WithInitializer( SyntaxFactory.EqualsValueClause( objCreation))))));This code creates an instance of the ObjectCreationExpressionSyntax class to represent the invocation of the MyClass constructor, and then uses it to create an instance of the VariableDeclaratorSyntax class that represents the declaration of the obj variable. Package library: Microsoft.CodeAnalysis.CSharp.Syntax.