This file contains 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
public class Sample | |
{ | |
public void Foo() | |
{ | |
Console.WriteLine(); | |
} | |
} |
This file contains 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
public class EmtpyStatementRemoval : CSharpSyntaxRewriter | |
{ | |
public override SyntaxNode VisitEmptyStatement(EmptyStatementSyntax node) | |
{ | |
//Construct an EmptyStatementSyntax with a missing semicolon | |
return node.WithSemicolonToken( | |
SyntaxFactory.MissingToken(SyntaxKind.SemicolonToken) | |
.WithLeadingTrivia(node.SemicolonToken.LeadingTrivia) | |
.WithTrailingTrivia(node.SemicolonToken.TrailingTrivia)); | |
} |
This file contains 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
public class Sample | |
{ | |
public void Foo() | |
{ | |
Console.WriteLine(); | |
#region SomeRegion | |
//Some other code | |
#endregion | |
} |
This file contains 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
string solutionPath = @"C:\Users\...\PathToSolution\MySolution.sln"; | |
var msWorkspace = MSBuildWorkspace.Create(); | |
var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result; | |
foreach (var project in solution.Projects) | |
{ | |
foreach (var document in project.Documents) | |
{ | |
Console.WriteLine(project.Name + "\t\t\t" + document.Name); | |
} |
This file contains 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 workspace = new AdhocWorkspace(); | |
string projName = "NewProject"; | |
var projectId = ProjectId.CreateNewId(); | |
var versionStamp = VersionStamp.Create(); | |
var projectInfo = ProjectInfo.Create(projectId, versionStamp, projName, projName, LanguageNames.CSharp); | |
var newProject = workspace.AddProject(projectInfo); | |
var sourceText = SourceText.From("class A {}"); | |
var newDocument = workspace.AddDocument(newProject.Id, "NewFile.cs", sourceText); |
This file contains 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
protected override void Initialize() | |
{ | |
//Other stuff... | |
... | |
var componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel)); | |
var workspace = componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>(); | |
} | |
//Alternatively you can MEF import the workspace. This has given me a lot of problems, |
This file contains 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
protected override void Initialize() | |
{ | |
//Other stuff... | |
... | |
var componentModel = (IComponentModel)this.GetService(typeof(SComponentModel)); | |
var workspace = componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>(); | |
} | |
//Alternatively you can MEF import the workspace. MEF can be tricky if you're not familiar with it |
This file contains 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
protected override void Initialize() | |
{ | |
//Other stuff... | |
... | |
var componentModel = (IComponentModel)this.GetService(typeof(SComponentModel)); | |
var workspace = componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>(); | |
} | |
//Alternatively you can MEF import the workspace. MEF can be tricky if you're not familiar with it |
This file contains 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 = CSharpSyntaxTree.ParseText(@" | |
public class MyClass | |
{ | |
int MyMethod() { return 0; } | |
}"); | |
var Mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); | |
var compilation = CSharpCompilation.Create("MyCompilation", | |
syntaxTrees: new[] { tree }, references: new[] { Mscorlib }); | |
//Note that we must specify the tree for which we want the model. |
This file contains 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 = CSharpSyntaxTree.ParseText(@" | |
public class MyClass { | |
int Method1() { return 0; } | |
void Method2() | |
{ | |
int x = Method1(); | |
} | |
} | |
}"); |