コード例 #1
0
        static async Task ProcessAsync(string code)
        {
            SyntaxTree tree = CSharpSyntaxTree.ParseText(code);
            var compilation = CSharpCompilation.Create("Sample")
                .AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
                .AddSyntaxTrees(tree);

            SemanticModel semanticModel = compilation.GetSemanticModel(tree);

            var propertyRewriter = new AutoPropertyRewriter(semanticModel);

            SyntaxNode root = await tree.GetRootAsync().ConfigureAwait(false);
            SyntaxNode rootWithAutoProperties = propertyRewriter.Visit(root);

            compilation = compilation.RemoveAllSyntaxTrees().AddSyntaxTrees(rootWithAutoProperties.SyntaxTree);
            semanticModel = compilation.GetSemanticModel(rootWithAutoProperties.SyntaxTree);
            var fieldRewriter = new RemoveBackingFieldRewriter(semanticModel, propertyRewriter.FieldsToRemove.ToArray());
            SyntaxNode rootWithFieldsRemoved = fieldRewriter.Visit(rootWithAutoProperties);
            WriteLine(rootWithFieldsRemoved);

        }
コード例 #2
0
        static async Task ProcessAsync(string code)
        {
            SyntaxTree tree        = CSharpSyntaxTree.ParseText(code);
            var        compilation = CSharpCompilation.Create("Sample")
                                     .AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
                                     .AddSyntaxTrees(tree);

            SemanticModel semanticModel = compilation.GetSemanticModel(tree);

            var propertyRewriter = new AutoPropertyRewriter(semanticModel);

            SyntaxNode root = await tree.GetRootAsync().ConfigureAwait(false);

            SyntaxNode rootWithAutoProperties = propertyRewriter.Visit(root);

            compilation   = compilation.RemoveAllSyntaxTrees().AddSyntaxTrees(rootWithAutoProperties.SyntaxTree);
            semanticModel = compilation.GetSemanticModel(rootWithAutoProperties.SyntaxTree);
            var        fieldRewriter         = new RemoveBackingFieldRewriter(semanticModel, propertyRewriter.FieldsToRemove.ToArray());
            SyntaxNode rootWithFieldsRemoved = fieldRewriter.Visit(rootWithAutoProperties);

            WriteLine(rootWithFieldsRemoved);
        }