Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using the visual studio project type - Stand-Alone Code Analysis Tool. I am using the following code but the ToString() is showing an unexpected result.

static void Main(string[] args)
        {
            var classDoc = @"public class SomeClass{
                    private SomeOtherClass someOtherClass;
                }";
            
            SyntaxTree classTree=SyntaxFactory.ParseSyntaxTree(classDoc);
            var classDecl = (ClassDeclarationSyntax)classTree.GetRoot().DescendantNodes().First(d => d is ClassDeclarationSyntax);
            var field = classDecl.Members.OfType<FieldDeclarationSyntax>().First();
            var fieldType = field.Declaration.Type;
            var newFieldType = SyntaxFactory.ParseName("System.Windows.Forms.UserControl");
            var newField=field.ReplaceNode(fieldType, newFieldType);
            var newFieldStr = newField.ToString();
        }

The newFieldStr is "private System.Windows.Forms.UserControlsomeOtherClass;"

Please advise how I can get the expected result.

Many thanks

Tony
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900