我试图使用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包含以下三个值:
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;
}
我的项目单独有“Condition1”指令。你能帮助我仅在条件单独有“Condition1”时才能获得代码吗?
提前致谢。
答案 0 :(得分:5)
您需要传递一个调用 08048ce1 <func4>: //not entirely sure what's happening here but it might be a binary search?
8048ce1: 83 ec 1c sub $0x1c,%esp
8048ce4: 89 5c 24 10 mov %ebx,0x10(%esp)
8048ce8: 89 74 24 14 mov %esi,0x14(%esp)
8048cec: 89 7c 24 18 mov %edi,0x18(%esp)
8048cf0: 8b 74 24 20 mov 0x20(%esp),%esi
8048cf4: 8b 5c 24 24 mov 0x24(%esp),%ebx
8048cf8: 85 f6 test %esi,%esi
8048cfa: 7e 2b jle 8048d27 <func4+0x46>
8048cfc: 83 fe 01 cmp $0x1,%esi
8048cff: 74 2b je 8048d2c <func4+0x4b>
8048d01: 89 5c 24 04 mov %ebx,0x4(%esp)
8048d05: 8d 46 ff lea -0x1(%esi),%eax
8048d08: 89 04 24 mov %eax,(%esp)
8048d0b: e8 d1 ff ff ff call 8048ce1 <func4>
8048d10: 8d 3c 18 lea (%eax,%ebx,1),%edi
8048d13: 89 5c 24 04 mov %ebx,0x4(%esp)
8048d17: 83 ee 02 sub $0x2,%esi
8048d1a: 89 34 24 mov %esi,(%esp)
8048d1d: e8 bf ff ff ff call 8048ce1 <func4>
8048d22: 8d 1c 07 lea (%edi,%eax,1),%ebx
8048d25: eb 05 jmp 8048d2c <func4+0x4b>
8048d27: bb 00 00 00 00 mov $0x0,%ebx
8048d2c: 89 d8 mov %ebx,%eax
8048d2e: 8b 5c 24 10 mov 0x10(%esp),%ebx
8048d32: 8b 74 24 14 mov 0x14(%esp),%esi
8048d36: 8b 7c 24 18 mov 0x18(%esp),%edi
8048d3a: 83 c4 1c add $0x1c,%esp
8048d3d: c3 ret
的{{1}}来将您要定义的符号传递给解析器。