Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Roslyn Structural Duplication

Roslyn Structural Duplication is a small .NET console tool that demonstrates structural C# code transformation with the Roslyn compiler APIs.

The project reads a C# source file, parses it into a syntax tree, finds method declarations that have exactly one parameter, duplicates that parameter, assigns the duplicated parameter a derived name, formats the modified tree, and writes the result to Output.cs.

What Problem It Solves

Text-based source-code changes are fragile because C# syntax can vary widely across formatting styles, modifiers, attributes, generic types, nested classes, and expression-bodied methods.

This project solves that problem by using Roslyn's syntax tree model instead of string replacement. It shows how to safely inspect and rewrite C# source structure with compiler-aware APIs.

It is useful as a learning project or starting point for:

  • C# source-to-source transformation
  • Roslyn syntax tree traversal
  • Automated refactoring experiments
  • Static analysis tooling
  • Code generation prototypes
  • Understanding CSharpSyntaxRewriter

Stack

  • Language: C#
  • Runtime: .NET 9
  • Project type: Console application
  • Compiler platform: Roslyn
  • Main packages:
    • Microsoft.CodeAnalysis.CSharp
    • Microsoft.CodeAnalysis.CSharp.Workspaces
  • Formatting: Microsoft.CodeAnalysis.Formatting.Formatter
  • Build system: .NET SDK / MSBuild

Features

  • Accepts a C# source file path as a command-line argument.
  • Parses source code with CSharpSyntaxTree.ParseText.
  • Traverses code using CSharpSyntaxRewriter.
  • Detects MethodDeclarationSyntax nodes with exactly one parameter.
  • Duplicates the existing parameter structurally.
  • Generates a new parameter name:
    • name becomes name2
    • name2 becomes name3
    • count9 becomes count10
    • param007 becomes param8
  • Preserves trivia from the original identifier.
  • Formats the transformed syntax tree.
  • Writes the transformed file to Output.cs.

Project Structure

.
├── RoslynStructuralDuplication.sln
├── README.md
└── RoslynStructuralDuplication/
    ├── RoslynStructuralDuplication.csproj
    ├── Program.cs      # Roslyn rewriter and CLI entry point
    └── Input.cs        # Sample input file with transformation cases

Build

dotnet build

Run

From the solution root:

dotnet run --project RoslynStructuralDuplication -- RoslynStructuralDuplication/Input.cs

The transformed file is written to:

Output.cs

Example

Input:

class Test
{
    static void Ping(string host) => Console.WriteLine(host);
}

Output:

class Test
{
    static void Ping(string host, string host2) => Console.WriteLine(host);
}

Current Scope

The rewriter currently targets only MethodDeclarationSyntax nodes with exactly one parameter.

It does not transform:

  • Constructors
  • Local functions
  • Methods with zero parameters
  • Methods with two or more parameters

Because this is a structural transformation demo, some duplicated parameters can require additional semantic handling before the output compiles. For example, duplicating params, out, or default-valued parameters may need extra rules depending on the final refactoring goal.

About

Roslyn-based .NET tool that rewrites C# syntax trees to duplicate single method parameters and generate formatted transformed source code.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages