代码改变世界

ilspy 点击根节点后进行解析的方法

2012-03-25 17:22  symphony2010  阅读(217)  评论(0编辑  收藏  举报

 

主要执行对根节点反编译的功能为以下函数:

image

函数中主要的代码:

   1:      
   2:                  // don't automatically load additional assemblies when an assembly node is selected in the tree view
   3:                  using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) {
   4:                      AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.AssemblyDefinition.MainModule);
   5:                      codeDomBuilder.AddAssembly(assembly.AssemblyDefinition, onlyAssemblyLevel: !options.FullDecompilation);
   6:                      codeDomBuilder.RunTransformations(transformAbortCondition);
   7:                      codeDomBuilder.GenerateCode(output);
   8:                  }
5行实现添加程序集,
6行实现什么功能?
分析如下:
 
 
   1:  public void RunTransformations(Predicate<IAstTransform> transformAbortCondition)
   2:          {
   3:              TransformationPipeline.RunTransformationsUntil(astCompileUnit, transformAbortCondition, context);
   4:              transformationsHaveRun = true;
   5:          }
 

传递的参数是一个“转换失败时中止的条件”,2行字面分析要执行转换单元,3行标识转换工作已经运行

可以先看以下链接的关于反编译管道详细介绍,

Daniel Grunwald 博客链接Decompiler Architecture Overview

   

继续分析TransformationPipeline这个类:

1:  public static class TransformationPipeline
   2:      {
   3:          public static IAstTransform[] CreatePipeline(DecompilerContext context)
   4:          {
   5:              return new IAstTransform[] {
   6:                  new PushNegation(),
   7:                  new DelegateConstruction(context),
   8:                  new PatternStatementTransform(context),
   9:                  new ReplaceMethodCallsWithOperators(context),
  10:                  new IntroduceUnsafeModifier(),
  11:                  new AddCheckedBlocks(),
  12:                  new DeclareVariables(context), // should run after most transforms that modify statements
  13:                  new ConvertConstructorCallIntoInitializer(), // must run after DeclareVariables
  14:                  new DecimalConstantTransform(),
  15:                  new IntroduceUsingDeclarations(context),
  16:                  new IntroduceExtensionMethods(context), // must run after IntroduceUsingDeclarations
  17:                  new IntroduceQueryExpressions(context), // must run after IntroduceExtensionMethods
  18:                  new CombineQueryExpressions(context),
  19:                  new FlattenSwitchBlocks(), 
  20:              };
  21:          }
  22:          
  23:          public static void RunTransformationsUntil(AstNode node, Predicate<IAstTransform> abortCondition, DecompilerContext context)
  24:          {
  25:              if (node == null)
  26:                  return;
  27:              
  28:              foreach (var transform in CreatePipeline(context)) {
  29:                  context.CancellationToken.ThrowIfCancellationRequested();
  30:                  if (abortCondition != null && abortCondition(transform))
  31:                      return;
  32:                  transform.Run(node);
  33:              }
  34:          }
  35:      }
结合博客的解释,可以得出,以上第六行的代码的功能便是对AstNode (Abstract Source Tree Node)进行转换管道的处理,最终处理为便于阅读的C#代码。