Kirill Osenkov blogged Roslyn Code Quoter tool – generating syntax API calls to generate any C# program and put it here.
Whether you’ve played with the Roslyn CTP or are planning to do so in the future, chances are that sooner or later you’ll run into a need to generate code.
I took out the chewy bits and worked up a PowerShell version.
Try It
Here I’m passing in a simple string of C#.
.\quoting.ps1 "var x = 1;"
Now that the Roslyn Code Quoter is plugged into the PowerShell ecosystem, you could easily recursively search a directory of C# files, crack them open and pass the contents top the code quoter.
dir . -Recurse *.cs | % { .\quoter.ps1 (Get-Content $_.FullName) }
See More
My book “Windows PowerShell for Developers” was released. Check it out, I have more examples integrating Microsoft Roslyn CTP and PowerShell plus another couple hundred pages of how Developers can ramp up PowerShell.
- Slice and dice Text, XML, CSV, and JSON effortlessly
- Embed PowerShell to provide scripting abilities for your C# apps
- Create GUI applications five to ten times faster with less code
- Leverage PowerShell’s capabilities to work with the Internet
- Interact with DLLs and create objects, automatically display properties, and call methods in live interactive sessions
- Build domain-specific languages (DSLs) and vocabularies to express solutions more clearly
- Work with Microsoft Office via the Component Object Model (COM)
- Discover PowerShell v3 features included with Windows 8 and Windows Server 2012
Results of the Code Quoter
Source : var x = 1;
Evaluated : var x = 1;
GeneratedCode : Syntax.CompilationUnit()
.WithMembers(
Syntax.List(
Syntax.FieldDeclaration(
Syntax.VariableDeclaration(
Syntax.IdentifierName(
Syntax.Identifier(
@"var",
Syntax.TriviaList(
Syntax.Space))))
.WithVariables(
Syntax.SeparatedList(
Syntax.VariableDeclarator(
Syntax.Identifier(
@"x",
Syntax.TriviaList(
Syntax.Space)))
.WithInitializer(
Syntax.EqualsValueClause(
Syntax.LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Syntax.Literal(
Syntax.TriviaList(),
@"1",
1,
Syntax.TriviaList())))
.WithEqualsToken(
Syntax.Token(
SyntaxKind.EqualsToken,
Syntax.TriviaList(
Syntax.Space)))))))
.WithSemicolonToken(
Syntax.Token(
SyntaxKind.SemicolonToken))))
.WithEndOfFileToken(
Syntax.Token(
SyntaxKind.EndOfFileToken))
AreEqual : True





{ 0 comments… add one now }