Exemplo n.º 1
0
        public override void DecompileMethod(IMethod method, ITextOutput output, DecompilationOptions options)
        {
            PEFile assembly = method.ParentModule.PEFile;

            AddReferenceAssemblyWarningMessage(assembly, output);
            AddReferenceWarningMessage(assembly, output);
            WriteCommentLine(output, TypeToString(method.DeclaringType, includeNamespace: true));
            CSharpDecompiler decompiler = CreateDecompiler(assembly, options);
            var methodDefinition        = decompiler.TypeSystem.MainModule.ResolveEntity(method.MetadataToken) as IMethod;

            if (methodDefinition.IsConstructor && methodDefinition.DeclaringType.IsReferenceType != false)
            {
                var members = CollectFieldsAndCtors(methodDefinition.DeclaringTypeDefinition, methodDefinition.IsStatic);
                decompiler.AstTransforms.Add(new SelectCtorTransform(methodDefinition));
                WriteCode(output, options.DecompilerSettings, decompiler.Decompile(members), decompiler.TypeSystem);
            }
            else
            {
                WriteCode(output, options.DecompilerSettings, decompiler.Decompile(method.MetadataToken), decompiler.TypeSystem);
            }
        }
Exemplo n.º 2
0
        public override void DecompileField(IField field, ITextOutput output, DecompilationOptions options)
        {
            PEFile assembly = field.ParentModule.PEFile;

            AddReferenceAssemblyWarningMessage(assembly, output);
            AddReferenceWarningMessage(assembly, output);
            WriteCommentLine(output, TypeToString(field.DeclaringType, includeNamespace: true));
            CSharpDecompiler decompiler = CreateDecompiler(assembly, options);

            if (field.IsConst)
            {
                WriteCode(output, options.DecompilerSettings, decompiler.Decompile(field.MetadataToken), decompiler.TypeSystem);
            }
            else
            {
                var members       = CollectFieldsAndCtors(field.DeclaringTypeDefinition, field.IsStatic);
                var resolvedField = decompiler.TypeSystem.MainModule.GetDefinition((FieldDefinitionHandle)field.MetadataToken);
                decompiler.AstTransforms.Add(new SelectFieldTransform(resolvedField));
                WriteCode(output, options.DecompilerSettings, decompiler.Decompile(members), decompiler.TypeSystem);
            }
        }
Exemplo n.º 3
0
        public DecompilationResult Decompile <TRes>(Func <ICursor, RedisValue[], RedisKey[], TRes> action)
            where TRes : RedResult
        {
            var token  = action.Method.MetadataToken;
            var method = MetadataTokenHelpers.TryAsEntityHandle(token);

            var ast = _decompiler.Decompile(new List <EntityHandle>()
            {
                method.Value
            });

            return(ExtractTreeAndMetadata(ast));
        }
Exemplo n.º 4
0
        public override void DecompileType(TypeDefinition type, ITextOutput output, DecompilationOptions options)
        {
            AddReferenceWarningMessage(type.Module.Assembly, output);
            WriteCommentLine(output, TypeToString(type, includeNamespace: true));
            output.WriteLine("namespace " + type.Namespace);
            output.WriteLine("{");
            output.Indent();
            CSharpDecompiler decompiler = CreateDecompiler(type.Module, options);

            WriteCode(output, options.DecompilerSettings, decompiler.Decompile(type), decompiler.TypeSystem);
            output.Unindent();
            output.WriteLine("}");
            output.WriteLine("");
        }
Exemplo n.º 5
0
 public override void Disassemble(PEFile module, MethodDefinitionHandle handle)
 {
     try {
         var csharpOutput            = new StringWriter();
         CSharpDecompiler decompiler = CreateDecompiler(module, options);
         var st = decompiler.Decompile(handle);
         WriteCode(csharpOutput, options.DecompilerSettings, st, decompiler.TypeSystem);
         var mapping = decompiler.CreateSequencePoints(st).FirstOrDefault(kvp => (kvp.Key.MoveNextMethod ?? kvp.Key.Method).MetadataToken == handle);
         this.sequencePoints = mapping.Value ?? (IList <SequencePoint>)EmptyList <SequencePoint> .Instance;
         this.codeLines      = csharpOutput.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
         base.Disassemble(module, handle);
     } finally {
         this.sequencePoints = null;
         this.codeLines      = null;
     }
 }
Exemplo n.º 6
0
            public override void Disassemble(MethodBody body)
            {
                var method = body.Method;

                try {
                    var csharpOutput            = new StringWriter();
                    CSharpDecompiler decompiler = CreateDecompiler(method.Module, options);
                    var st = decompiler.Decompile(method);
                    WriteCode(csharpOutput, options.DecompilerSettings, st, decompiler.TypeSystem);
                    var mapping = decompiler.CreateSequencePoints(st).FirstOrDefault(kvp => kvp.Key.CecilMethod == method);
                    this.sequencePoints = mapping.Value ?? (IList <IL.SequencePoint>)EmptyList <IL.SequencePoint> .Instance;
                    this.codeLines      = csharpOutput.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                    base.Disassemble(body);
                } finally {
                    this.sequencePoints = null;
                    this.codeLines      = null;
                }
            }
Exemplo n.º 7
0
        public SyntaxTree Read(Delegate @delegate)
        {
            Contract.Assert(@delegate?.Method?.DeclaringType != null);

            var asm = @delegate.Method.DeclaringType.Assembly;

            _assemblyProvider.Prepare(asm);
            var decompiler = new CSharpDecompiler(asm.Location, _assemblyProvider, new DecompilerSettings()
            {
                ExtensionMethods = false,
                NamedArguments   = false
            });
            var token  = @delegate.Method.MetadataToken;
            var method = MetadataTokenHelpers.TryAsEntityHandle(token);
            var ast    = decompiler.Decompile(new List <EntityHandle>()
            {
                method.Value
            });

            return(ast);
        }