Ejemplo n.º 1
0
        public override RichText GetRichTextTooltip(IEntity entity)
        {
            var output = new AvalonEditTextOutput()
            {
                IgnoreNewLineAndIndent = true
            };
            var disasm = CreateDisassembler(output, new DecompilationOptions());

            switch (entity.SymbolKind)
            {
            case SymbolKind.TypeDefinition:
                disasm.DisassembleTypeHeader(entity.ParentModule.PEFile, (TypeDefinitionHandle)entity.MetadataToken);
                break;

            case SymbolKind.Field:
                disasm.DisassembleFieldHeader(entity.ParentModule.PEFile, (FieldDefinitionHandle)entity.MetadataToken);
                break;

            case SymbolKind.Property:
            case SymbolKind.Indexer:
                disasm.DisassemblePropertyHeader(entity.ParentModule.PEFile, (PropertyDefinitionHandle)entity.MetadataToken);
                break;

            case SymbolKind.Event:
                disasm.DisassembleEventHeader(entity.ParentModule.PEFile, (EventDefinitionHandle)entity.MetadataToken);
                break;

            case SymbolKind.Method:
            case SymbolKind.Operator:
            case SymbolKind.Constructor:
            case SymbolKind.Destructor:
            case SymbolKind.Accessor:
                disasm.DisassembleMethodHeader(entity.ParentModule.PEFile, (MethodDefinitionHandle)entity.MetadataToken);
                break;

            default:
                output.Write(GetDisplayName(entity, true, true, true));
                break;
            }

            return(new DocumentHighlighter(output.GetDocument(), base.SyntaxHighlighting).HighlightLine(1).ToRichText());
        }
Ejemplo n.º 2
0
        public ICSharpCode.AvalonEdit.Document.TextDocument Decompile(object obj)
        {
            AvalonEditTextOutput aeto = new AvalonEditTextOutput();
            AstBuilder           ast  = new AstBuilder(new DecompilerContext(ModuleDefinition.CreateModule("ash", ModuleKind.NetModule)));

            switch (obj.GetType().Name)
            {
            case "AssemblyDefinition":
                ast = new AstBuilder(new DecompilerContext((obj as AssemblyDefinition).MainModule)
                {
                    Settings = new DecompilerSettings()
                });
                try { ast.AddAssembly(obj as AssemblyDefinition); }
                catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.FullName); }
                break;

            case "TypeDefinition":
                ast = CreateAstBuilder((obj as TypeDefinition), true);
                try { ast.AddType(obj as TypeDefinition); }
                catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.FullName); }
                break;

            case "MethodDefinition":
                MethodDefinition method = (obj as MethodDefinition);
                ast = CreateAstBuilder(method.DeclaringType, true);
                if (method.IsConstructor && !method.IsStatic && !method.DeclaringType.IsValueType)
                {
                    foreach (var field in method.DeclaringType.Fields)
                    {
                        if (field.IsStatic == method.IsStatic)
                        {
                            try { ast.AddField(field); }
                            catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
                        }
                    }
                    foreach (var ctor in method.DeclaringType.Methods)
                    {
                        if (ctor.IsConstructor && ctor.IsStatic == method.IsStatic)
                        {
                            try { ast.AddMethod(ctor); }
                            catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
                        }
                    }
                }
                else
                {
                    try { ast.AddMethod(obj as MethodDefinition); }
                    catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
                }
                break;

            case "PropertyDefinition":
                ast = CreateAstBuilder((obj as PropertyDefinition).DeclaringType, true);
                try { ast.AddProperty(obj as PropertyDefinition); }
                catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
                break;

            case "FieldDefinition":
                ast = CreateAstBuilder((obj as FieldDefinition).DeclaringType, true);
                try { ast.AddField(obj as FieldDefinition); }
                catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
                break;

            case "EventDefinition":
                ast = CreateAstBuilder((obj as EventDefinition).DeclaringType, true);
                try { ast.AddEvent(obj as EventDefinition); }
                catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
                break;

            default:
                return(new ICSharpCode.AvalonEdit.Document.TextDocument());
            }
            try { ast.GenerateCode(aeto); }
            catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly upon code generation:\r" + e.AssemblyReference.FullName); }
            return(aeto.GetDocument());
        }