#if 指令代码在 roslyn 中解析

#if directive code parsing in roslyn

我正在尝试使用 Roslyn 从我的 CS 文件中提取部分代码,但我遇到了以下问题。

我的C#文件代码:

    class ConditionalCompilationCode
    {
#if Condition2
            int test2=0; 
#endif
#if Condition1
            int test1=0; 
#endif

        public static void Main1(string[] args)
        {
            int test = 0;
#if Condition1
            test = 1;
#else
            test =2;
#endif

#if Condition2
            test =3;
#else
            test = 4;
#endif

        }
#if Condition2
            private void testmethod1()
        {
            test2 = 1;
        }
#endif
#if !Condition2
        private void testmethod2()
        {
            test1 = 1;
        }
#endif
#if Condition1
        private void testmethod3()
        {
            test1 = 1;
        }
#endif
#if !Condition1
            private void testmethod4()
        {
            test2 = 1;
        }
#endif
    }
}

我的roslyn代码:

string fileContent = File.ReadAllText(fileName);

SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(fileContent);

var syntaxRootNode = syntaxTree.GetRoot();
            if (syntaxRootNode.GetFirstToken().Kind() == SyntaxKind.None)
                return;
            foreach (NamespaceDeclarationSyntax namespaceSyntax in syntaxRootNode.DescendantNodes().OfType<NamespaceDeclarationSyntax>().ToArray())
            {

                IEnumerable<SyntaxNode> nodeList = namespaceSyntax.ChildNodes();
                string className = null;
                foreach (SyntaxNode syntaxNode in nodeList)
                {
                    SyntaxKind kind = syntaxNode.Kind();
                    switch (kind)
                    {
                        case SyntaxKind.ClassDeclaration:
                            UpdateClassSignature(syntaxNode as TypeDeclarationSyntax);                            
                            break;
                        case SyntaxKind.EnumDeclaration:

                            break;
                        case SyntaxKind.InterfaceDeclaration:

                            break;
                        case SyntaxKind.StructDeclaration:

                            break;

                    }

                }

            }

    private void UpdateClassSignature(TypeDeclarationSyntax classDeclarationSyntax)
        {

            foreach (MemberDeclarationSyntax member in classDeclarationSyntax.Members)
            {
                SyntaxKind kind = member.Kind();
                switch (kind)
                {
                    case SyntaxKind.FieldDeclaration:
                        break;
                    case SyntaxKind.PropertyDeclaration:
                        break;
                    case SyntaxKind.MethodDeclaration:
                    case SyntaxKind.ConstructorDeclaration:
                    case SyntaxKind.DestructorDeclaration:
                        break;
                    case SyntaxKind.IndexerDeclaration:
                        ExtractIndexer(member as IndexerDeclarationSyntax, classSign);
                        break;
                    case SyntaxKind.DelegateDeclaration:
                        //TODO: Add Delegate Support.
                        break;
                    case SyntaxKind.OperatorDeclaration:
                    case SyntaxKind.ConversionOperatorDeclaration:
                        //Skip.
                        //TODO: Need to add operator suport.
                        break;
                    case SyntaxKind.EventFieldDeclaration:
                        //TODO: Add support to event.
                        break;
                    case SyntaxKind.EventDeclaration:
                        //TODO: Add support to event.
                        break;
                    case SyntaxKind.EnumDeclaration:
                        break;
                    case SyntaxKind.ClassDeclaration:
                        break;
                    case SyntaxKind.StructDeclaration:
                        break;
                    default:
                        break;
                }
            }
        }

使用roslyn处理上述CS文件时,在classDeclarationSyntax.Members中包含以下三个值:

  1. MethodDeclarationSyntax MethodDeclaration

    public static void Main1(string[] args)
    {
        int test = 0;
        #if Condition1
        test = 1;
        #else
        test =2;
        #endif
    
       #if Condition2
        test =3;
       #else
        test = 4;
      #endif
    
    }
    
  2. MethodDeclarationSyntax MethodDeclaration

    private void testmethod2()
    {
        test1 = 1;
    }
    
  3. MethodDeclarationSyntax MethodDeclaration

    private void testmethod4()
    {
        test2 = 1;
    }
    

我的项目只有 "Condition1" 指令。你能帮我只在条件只有 "Condition1" 的情况下获取代码吗?

提前致谢。

您需要传递一个 CSharpParseOptions 来调用 WithPreprocessorSymbols() 以将您要定义的符号传递给解析器。