1

我有一个 *.il 文件。我想在其中找到所有非空方法(.method)。例如:

.class private auto ansi beforefieldinit MyApp.Program
       extends [mscorlib]System.Object
{
   //catch its body
  .method private hidebysig static void  Main(string[] args) cil managed
  {
    .entrypoint
    // 
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ret
  }  

  //catch its body
  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // 
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  }  

   //don't touch, it's empty
   .method public hidebysig newslot virtual 
          instance string  Invoke(string a) runtime managed
  {
  }  
 //......................................
}

现在我正在使用类字符串来做到这一点。这很不合理。我尝试使用Regex,但我不知道如何创建一个 reg 表达式来仅捕获

  • 方法(而不是类)
  • 仅具有非空主体的方法

有人可以帮助我吗?

4

1 回答 1

1

Using regex for parsing structure code is not recommended and it is bad practice

Try to use regex pattern

(\.method\s[^{]+?)(?=\s*{)(?!\s*{\s*})

Test it here.

To catch also the body {...} of each method, use regex pattern

(\.method\s[^{]+{(?!\s*}).*?})

Test it here.


To learn more about regular expressions visit regular-expressions.info

于 2012-10-21T16:02:15.333 回答