Skip to content

Instantly share code, notes, and snippets.

@DinisCruz
Created June 11, 2012 17:59
Show Gist options
  • Save DinisCruz/2911643 to your computer and use it in GitHub Desktop.
Save DinisCruz/2911643 to your computer and use it in GitHub Desktop.
O2 Script - Refactored Roslyn 'AddingMethodToClass' method
var code = @"
class C
{
}";
// Get ClassDeclarationSyntax corresponding to 'class C' above.
var classDeclaration = code.astTree()
.compilationUnit()
.classes().first();
// Construct a new MethodDeclarationSyntax.
// Add this new MethodDeclarationSyntax to the above ClassDeclarationSyntax.
var newClassDeclaration = classDeclaration.add("M".methodDeclaration());
// Update the CompilationUnitSyntax with the new ClassDeclarationSyntax.
// Format the new CompilationUnitSyntax.
return classDeclaration.replaceWith(newClassDeclaration).formatedCode();
//O2Ref:O2_FluentSharp_Roslyn.dll
//O2File:_Extra_methods_Roslyn_API.cs
//O2Ref:O2_FluentSharp_Roslyn.dll
//O2Ref:Roslyn.Services.dll
//O2Ref:Roslyn.Compilers.dll
//O2Ref:Roslyn.Compilers.CSharp.dll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment