This project is read-only.
1
Vote

Issue in using directive codes

description

0
down vote
favorite
Im trying to extract the part of code from my CS file using Roslyn and Im suffering the below problem.

My C# file code:
    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
    }
}
My roslyn code:
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;
                }
            }
        }
While processing the above CS file using roslyn, in the classDeclarationSyntax.Members contains the following three values:

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

}
MethodDeclarationSyntax MethodDeclaration
private void testmethod2()
{
    test1 = 1;
}
MethodDeclarationSyntax MethodDeclaration
private void testmethod4()
{
    test2 = 1;
}
My project has "Condition1" directive alone and the actual methods are Main1, testmethod3 and testmethod4. Can you help me to get the codes only when the condition has "Condition1" alone?

comments

PauloMorgado wrote Nov 15, 2016 at 11:58 PM

Questions on how to use Roslyn have moved to Stack Overflow and MSDN Forums