Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // <SnippetDeclareTestCompilation>
            Compilation test = CreateTestCompilation();

            // </SnippetDeclareTestCompilation>

            // <SnippetIterateTrees>
            foreach (SyntaxTree sourceTree in test.SyntaxTrees)
            {
                SemanticModel model = test.GetSemanticModel(sourceTree);

                TypeInferenceRewriter rewriter = new TypeInferenceRewriter(model);

                // <SnippetTransformTrees>
                SyntaxNode newSource = rewriter.Visit(sourceTree.GetRoot());

                if (newSource != sourceTree.GetRoot())
                {
                    File.WriteAllText(sourceTree.FilePath, newSource.ToFullString());
                }
                // </SnippetTransformTrees>
            }
            // </SnippetIterateTrees>
        }
Exemplo n.º 2
0
        static async Task TransformQuickStart(string[] args)
        {
            // var test = CreateTestCompilation();

            var solutionPath = @"..\..\..\..\SyntaxTransformationQuickStart.sln";

            MSBuildLocator.RegisterDefaults();
            var msWorkspace = MSBuildWorkspace.Create();

            var originalSolution = await msWorkspace.OpenSolutionAsync(solutionPath);

            var newSolution = originalSolution;

            foreach (ProjectId projectId in originalSolution.ProjectIds)
            {
                var newProject = newSolution.GetProject(projectId);

                foreach (var documentId in newProject.DocumentIds)
                {
                    var document = newSolution.GetDocument(documentId);

                    var tree = await document.GetSyntaxTreeAsync();

                    var model = await document.GetSemanticModelAsync();

                    var rewriter = new TypeInferenceRewriter(model);

                    var newSource = rewriter.Visit(tree.GetRoot());

                    var newDocument = document.WithText(newSource.GetText());

                    newSolution = newDocument.Project.Solution;
                }
            }

            if (msWorkspace.TryApplyChanges(newSolution))
            {
                Console.WriteLine("Solution updated.");
            }
            else
            {
                Console.WriteLine("Update failed!");
            }

            //foreach (SyntaxTree sourceTree in test.SyntaxTrees)
            //{
            //    var model = test.GetSemanticModel(sourceTree);

            //    var rewriter = new TypeInferenceRewriter(model);

            //    var newSource = rewriter.Visit(sourceTree.GetRoot());

            //    if (newSource != sourceTree.GetRoot())
            //    {
            //        File.WriteAllText(sourceTree.FilePath, newSource.ToFullString());
            //    }
            //}
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var test = CreateTestCompilation();

            foreach (var sourceTree in test.SyntaxTrees)
            {
                var model     = test.GetSemanticModel(sourceTree);
                var rewriter  = new TypeInferenceRewriter(model);
                var newSource = rewriter.Visit(sourceTree.GetRoot());
                if (newSource != sourceTree.GetRoot())
                {
                    File.WriteAllText(sourceTree.FilePath, newSource.ToFullString());
                }
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Compilation test = CreateTestCompilation();

            foreach (SyntaxTree sourceTree in test.SyntaxTrees)
            {
                SemanticModel model = test.GetSemanticModel(sourceTree);

                TypeInferenceRewriter rewriter = new TypeInferenceRewriter(model);

                SyntaxNode newSource = rewriter.Visit(sourceTree.GetRoot());

                if (newSource != sourceTree.GetRoot())
                {
                    File.WriteAllText(sourceTree.FilePath, newSource.ToFullString());
                }
            }
        }
Exemplo n.º 5
0
        static async Task TransformTest(string[] args)
        {
            var test = CreateTestCompilation();

            foreach (SyntaxTree sourceTree in test.SyntaxTrees)
            {
                var model = test.GetSemanticModel(sourceTree);

                var rewriter = new TypeInferenceRewriter(model);

                var newSource = rewriter.Visit(sourceTree.GetRoot());

                if (newSource != sourceTree.GetRoot())
                {
                    File.WriteAllText(sourceTree.FilePath, newSource.ToFullString());
                }
            }

            await Task.CompletedTask;
        }