using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Build.Locator; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.MSBuild; using Microsoft.CodeAnalysis.Text;
namespaceTestMyRewriter { classProgram { staticasync Task Main(string[] args) { //原始代码 string strCode = @" public class Foo { public string _bar = ""baz""; public string strHello = ""heloo world""; }"; var tree = CSharpSyntaxTree.ParseText(strCode); var Mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); var compilation = CSharpCompilation.Create("MyCompilation", syntaxTrees: new[] { tree }, references: new[] { Mscorlib }); // 获得语义模型 var model = compilation.GetSemanticModel(tree); var root = model.SyntaxTree.GetRoot(); // 用Visit重写代码 var rw = new LiteralRewriter(); var newRoot = rw.Visit(root); // 新生成代码 string strNewCode = newRoot.GetText().ToString(); Console.WriteLine(strNewCode); Console.ReadLine(); } }
if (!MSBuildLocator.IsRegistered) MSBuildLocator.RegisterDefaults(); using (var w = MSBuildWorkspace.Create()) { var solution = await w.OpenSolutionAsync(solutionPath); //await SolutionAttributeUpdater.UpdateAttributes(solution); } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Build.Locator; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.MSBuild; using Microsoft.CodeAnalysis.Text;
if (!MSBuildLocator.IsRegistered) MSBuildLocator.RegisterDefaults(); using (var w = MSBuildWorkspace.Create()) { var solution = await w.OpenSolutionAsync(solutionPath); //await SolutionAttributeUpdater.UpdateAttributes(solution); var project = solution.Projects.First(x => x.Name == "MyConsole");
var document = project.Documents.First(x => x.Name.Equals("Program.cs", StringComparison.InvariantCultureIgnoreCase));
var tree = await document.GetSyntaxTreeAsync(); var syntax = tree.GetCompilationUnitRoot();
var visitor = new TypeParameterVisitor(); var node = visitor.Visit(syntax);
var text = node.GetText(); File.WriteAllText(document.FilePath, text.ToString()); } } }
publicclassSolutionAttributeUpdater { publicstaticasync Task<Solution> UpdateAttributes(Solution solution) { foreach (var project in solution.Projects) { foreach (var document in project.Documents) { var syntaxTree = await document.GetSyntaxTreeAsync(); var root = syntaxTree.GetRoot();
var descentants = root.DescendantNodes().Where(curr => curr is AttributeListSyntax).ToList(); if (descentants.Any()) { var attributeList = SyntaxFactory.AttributeList( SyntaxFactory.SingletonSeparatedList( SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("CookiesAttribute"), SyntaxFactory.AttributeArgumentList(SyntaxFactory.SeparatedList(new[] { SyntaxFactory.AttributeArgument( SyntaxFactory.LiteralExpression( SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(@"SampleClass")) )})))));