Created
June 11, 2012 18:00
-
-
Save DinisCruz/2911647 to your computer and use it in GitHub Desktop.
O2 Script - Original Roslyn 'AddingMethodToClass' method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tree = SyntaxTree.ParseCompilationUnit(@" | |
class C | |
{ | |
}"); | |
var compilationUnit = (CompilationUnitSyntax)tree.GetRoot(); | |
// Get ClassDeclarationSyntax corresponding to 'class C' above. | |
ClassDeclarationSyntax classDeclaration = compilationUnit.ChildNodes() | |
.OfType<ClassDeclarationSyntax>().Single(); | |
// Construct a new MethodDeclarationSyntax. | |
MethodDeclarationSyntax newMethodDeclaration = | |
Syntax.MethodDeclaration(Syntax.ParseTypeName("void"), "M") | |
.WithBody(Syntax.Block()); | |
// Add this new MethodDeclarationSyntax to the above ClassDeclarationSyntax. | |
ClassDeclarationSyntax newClassDeclaration = | |
classDeclaration.AddMembers(newMethodDeclaration); | |
// Update the CompilationUnitSyntax with the new ClassDeclarationSyntax. | |
CompilationUnitSyntax newCompilationUnit = | |
compilationUnit.ReplaceNode(classDeclaration, newClassDeclaration); | |
// Format the new CompilationUnitSyntax. | |
newCompilationUnit = (CompilationUnitSyntax)newCompilationUnit.Format().GetFormattedRoot(); | |
return newCompilationUnit.GetFullText(); | |
//using Roslyn.Compilers; | |
//using Roslyn.Compilers.Common; | |
//using Roslyn.Compilers.CSharp; | |
//using Roslyn.Scripting; | |
//using Roslyn.Scripting.CSharp; | |
//using Roslyn.Services; | |
//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