示例#1
0
        /// <summary>
        /// Creates the object. If it's syntax highlighted, it's a <see cref="TextBlock"/>, else
        /// it's just a <see cref="string"/>. See also <see cref="CreateTextBlock()"/>
        /// </summary>
        /// <param name="useEllipsis">true to add <see cref="TextTrimming.CharacterEllipsis"/> to the <see cref="TextBlock"/></param>
        /// <param name="filterOutNewLines">true to filter out newline characters</param>
        /// <returns></returns>
        public object CreateObject(bool useEllipsis = false, bool filterOutNewLines = true)
        {
            if (simpleHighlighter != null)
            {
                return(simpleHighlighter.Create(useEllipsis, filterOutNewLines));
            }

            return(ToString(output.ToString(), filterOutNewLines));
        }
示例#2
0
        /// <summary>
        /// Creates the object. If it's syntax highlighted, it's a <see cref="TextBlock"/>, else
        /// it's just a <see cref="string"/>. See also <see cref="CreateTextBlock()"/>
        /// </summary>
        /// <returns></returns>
        public object CreateObject()
        {
            if (simpleHighlighter != null)
            {
                return(simpleHighlighter.Create());
            }

            return(output.ToString());
        }
示例#3
0
 private void GenerateCode(AstBuilder decompiler)
 {
     decompiler.GenerateCode(_output);
     if (!String.IsNullOrEmpty(OutputFileName))
     {
         File.WriteAllText(OutputFileName, _output.ToString());
     }
     else
     {
         _context.Write(_output.ToString());
     }
 }
示例#4
0
        protected override void CopyItemsAsText(LocalVM[] locals)
        {
            Array.Sort(locals, (a, b) => a.Index.CompareTo(b.Index));

            var output = new PlainTextOutput();

            for (int i = 0; i < locals.Length; i++)
            {
                if (i > 0)
                {
                    output.WriteLine();
                }

                var local = locals[i];
                output.Write(local.Index.ToString(), TextTokenType.Number);
                output.Write('\t', TextTokenType.Text);
                output.Write(local.IsPinned ? "P" : string.Empty, TextTokenType.Text);
                output.Write('\t', TextTokenType.Text);
                output.Write(local.IsCompilerGenerated ? "C" : string.Empty, TextTokenType.Text);
                output.Write('\t', TextTokenType.Text);
                output.Write(local.Name ?? string.Empty, TextTokenType.Local);
                output.Write('\t', TextTokenType.Text);
                BodyUtils.WriteObject(output, local.Type);
            }
            if (locals.Length > 1)
            {
                output.WriteLine();
            }

            Clipboard.SetText(output.ToString());
        }
示例#5
0
文件: Commands.cs 项目: xornand/dnSpy
        protected override void Execute(LocalsCtxMenuContext context)
        {
            var output = new PlainTextOutput();

            foreach (var vm in context.SelectedItems)
            {
                //TODO: Break if it takes too long and the user cancels
                var printer = new ValuePrinter(output, DebuggerSettings.Instance.UseHexadecimal);
                printer.WriteExpander(vm);
                output.Write('\t', TextTokenType.Text);
                // Add an extra here to emulate VS output
                output.Write('\t', TextTokenType.Text);
                printer.WriteName(vm);
                output.Write('\t', TextTokenType.Text);
                printer.WriteValue(vm);
                output.Write('\t', TextTokenType.Text);
                printer.WriteType(vm);
                output.WriteLine();
            }
            var s = output.ToString();

            if (s.Length > 0)
            {
                Clipboard.SetText(s);
            }
        }
示例#6
0
        public static string Disassemble <T>(this T member)
            where T : class, IMemberDefinition
        {
            var writer = new PlainTextOutput();
            var dasm   = new ReflectionDisassembler(writer);

            if (typeof(T) == typeof(EventDefinition))
            {
                dasm.DisassembleEvent(member as EventDefinition);
            }
            else if (typeof(T) == typeof(FieldDefinition))
            {
                dasm.DisassembleField(member as FieldDefinition);
            }
            else if (typeof(T) == typeof(PropertyDefinition))
            {
                dasm.DisassembleProperty(member as PropertyDefinition);
            }
            else if (typeof(T) == typeof(MethodDefinition))
            {
                dasm.DisassembleMethod(member as MethodDefinition);
            }
            else if (typeof(T) == typeof(TypeDefinition))
            {
                dasm.DisassembleType(member as TypeDefinition);
            }
            else
            {
                throw new NotImplementedException($"the type {typeof(T).FullName} cannot be disassembled.");
            }

            return(writer.ToString());
        }
示例#7
0
        public override string ToString()
        {
            var output = new PlainTextOutput();

            CreateUI(output, NameObject, false);
            return(output.ToString());
        }
示例#8
0
        public override string TypeToString(TypeReference t, bool includeNamespace, ICustomAttributeProvider attributeProvider)
        {
            PlainTextOutput output = new PlainTextOutput();

            t.WriteTo(output, true, shortName: !includeNamespace);
            return(output.ToString());
        }
示例#9
0
        public static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Please enter a file path");
                return(1);
            }

            var filePath = args[0];

            Console.WriteLine(filePath);
            if (!File.Exists(filePath))
            {
                Console.WriteLine("File not Exist");
                return(1);
            }

            var modDef = ModuleDefinition.ReadModule(args[0]);

            foreach (var typeDef in modDef.GetTypes())
            {
                Console.WriteLine(Environment.NewLine + typeDef.Name + Environment.NewLine);
                var builder = new AstBuilder(new DecompilerContext(modDef));
                builder.AddType(typeDef);
                builder.RunTransformations();
                var output = new PlainTextOutput();
                builder.GenerateCode(output);
                Console.Write(output.ToString());
            }

            return(0);
        }
        public override string TypeToString(TypeReference t, bool includeNamespace, ICustomAttributeProvider attributeProvider = null)
        {
            PlainTextOutput output = new PlainTextOutput();

            t.WriteTo(output, includeNamespace ? ILNameSyntax.TypeName : ILNameSyntax.ShortTypeName);
            return(output.ToString());
        }
示例#11
0
        public string ToString(Language language)
        {
            var output = new PlainTextOutput();

            Write(output, language);
            return(output.ToString());
        }
示例#12
0
 public static async Task <string> GetSourceCode(TypeDefinition typeDefinition)
 {
     return(await Task.Run(() =>
     {
         try
         {
             var settings = new DecompilerSettings {
                 UsingDeclarations = true
             };
             var context = new DecompilerContext(typeDefinition.Module)
             {
                 CurrentType = typeDefinition,
                 Settings = settings
             };
             var astBuilder = new AstBuilder(context);
             var textOutput = new PlainTextOutput();
             astBuilder.GenerateCode(textOutput);
             return textOutput.ToString();
         }
         catch (Exception ex)
         {
             return "Error in creating source code from Type: " + ex.Message + ex.Message + Environment.NewLine + ex.StackTrace;
         }
     }));
 }
示例#13
0
        Stream GetDecompiledStream(CancellationToken token)
        {
            var output = new PlainTextOutput();

            Decompile(output, token);
            return(ResourceUtils.StringToStream(output.ToString()));
        }
示例#14
0
        public static string Disassemble(this MethodBody body)
        {
            var writer = new PlainTextOutput();
            var dasm   = new MethodBodyDisassembler(writer);

            dasm.Disassemble(body);
            return(writer.ToString());
        }
示例#15
0
        public string Decompile(object @object)
        {
            if (@object == null)
            {
                return(String.Empty);
            }
            Language l = new CSharpLanguage();

            //ITextOutput output = new RtfTextOutput();
            ITextOutput output  = new PlainTextOutput();
            var         options = new DecompilationOptions();

            if (@object is AssemblyDefinition)
            {
                l.DecompileAssembly((AssemblyDefinition)@object, output, options);
            }
            else if (@object is TypeDefinition)
            {
                l.DecompileType((TypeDefinition)@object, output, options);
            }
            else if (@object is MethodDefinition)
            {
                l.DecompileMethod((MethodDefinition)@object, output, options);
            }
            else if (@object is FieldDefinition)
            {
                l.DecompileField((FieldDefinition)@object, output, options);
            }
            else if (@object is PropertyDefinition)
            {
                l.DecompileProperty((PropertyDefinition)@object, output, options);
            }
            else if (@object is EventDefinition)
            {
                l.DecompileEvent((EventDefinition)@object, output, options);
            }
            else if (@object is AssemblyNameReference)
            {
                output.Write("// Assembly Reference ");
                output.WriteDefinition(@object.ToString(), null);
                output.WriteLine();
            }
            else if (@object is ModuleReference)
            {
                output.Write("// Module Reference ");
                output.WriteDefinition(@object.ToString(), null);
                output.WriteLine();
            }
            else
            {
                output.Write(String.Format("// {0} ", @object.GetType().Name));
                output.WriteDefinition(@object.ToString(), null);
                output.WriteLine();
            }

            return(output.ToString());
        }
        private void SetupDecompilerAndOriginalSource(MethodDefinition method)
        {
            _cSharpDecompiler = new CSharpLanguage();
            var decompilationOutput = new PlainTextOutput();

            _decompilationOptions = new DecompilationOptions();
            _cSharpDecompiler.DecompileMethod(method, decompilationOutput, _decompilationOptions);
            _oldText = decompilationOutput.ToString();
            _differ  = new SideBySideDiffBuilder(new Differ());
        }
示例#17
0
        public override string ToString()
        {
            var output = new PlainTextOutput();

            WriteTo(output, new ILAstWritingOptions());
            if (!ILRange.IsEmpty)
            {
                output.Write(" at IL_" + ILRange.Start.ToString("x4"));
            }
            return(output.ToString());
        }
示例#18
0
 private void Disassemble(Stream peStream)
 {
     using (var assembly = AssemblyDefinition.ReadAssembly(peStream)) {
         var output       = new PlainTextOutput();
         var disassembler = new ReflectionDisassembler(output, false, CancellationToken.None);
         disassembler.WriteModuleContents(assembly.MainModule);
         Disassembled(new DisassembledMesssage {
             IL = output.ToString()
         });
     }
 }
示例#19
0
        public override string ToString(CancellationToken token, bool canDecompile)
        {
            if (!canDecompile)
            {
                return(null);
            }
            var output = new PlainTextOutput();

            Decompile(output, token);
            return(output.ToString());
        }
示例#20
0
文件: Commands.cs 项目: ottrur/dnSpy
		protected override void Execute(LocalsCtxMenuContext context) {
			var output = new PlainTextOutput();
			foreach (var vm in context.SelectedItems) {
				//TODO: Break if it takes too long and the user cancels
				var printer = new ValuePrinter(output, DebuggerSettings.Instance.UseHexadecimal);
				printer.WriteValue(vm);
				if (context.SelectedItems.Length > 1)
					output.WriteLine();
			}
			var s = output.ToString();
			if (s.Length > 0)
				Clipboard.SetText(s);
		}
示例#21
0
        static void ShowIL(string assemblyFileName, TextWriter output, string[] referencePaths)
        {
            CSharpDecompiler       decompiler   = GetDecompiler(assemblyFileName, referencePaths);
            ITextOutput            textOutput   = new PlainTextOutput();
            ReflectionDisassembler disassembler = new ReflectionDisassembler(textOutput, CancellationToken.None);

            disassembler.DisassembleNamespace(decompiler.TypeSystem.MainModule.RootNamespace.Name,
                                              decompiler.TypeSystem.MainModule.PEFile,
                                              decompiler.TypeSystem.MainModule.TypeDefinitions.Select(x => (TypeDefinitionHandle)x.MetadataToken));

            output.WriteLine($"// IL code: {decompiler.TypeSystem.MainModule.AssemblyName}");
            output.WriteLine(textOutput.ToString());
        }
        private static string GenerateAssemblyDescription(ModuleDefinition module)
        {
            var context    = new DecompilerContext(module);
            var astBuilder = new AstBuilder(context)
            {
                DecompileMethodBodies = false
            };

            astBuilder.AddAssembly(module, true);
            var output = new PlainTextOutput();

            astBuilder.GenerateCode(output);
            return(output.ToString());
        }
    public static string ToSourceCode(this Type source)
    {
        var assembly = AssemblyDefinition.ReadAssembly(Assembly.GetExecutingAssembly().Location);
        var type     = assembly.MainModule.Types.FirstOrDefault(t => t.FullName == source.FullName);

        if (type == null)
        {
            return(string.Empty);
        }
        var plainTextOutput = new PlainTextOutput();
        var decompiler      = new CSharpLanguage();

        decompiler.DecompileType(type, plainTextOutput, new DecompilationOptions());
        return(Regex.Replace(Regex.Replace(plainTextOutput.ToString(), @"\n|\r", " "), @"\s+", " "));
    }
示例#24
0
        public override void Execute(MDTableContext context)
        {
            var output = new PlainTextOutput();

            context.TreeNode.WriteHeader(output);
            foreach (var rec in context.Records)
            {
                context.TreeNode.Write(output, rec);
            }
            var s = output.ToString();

            if (s.Length > 0)
            {
                Clipboard.SetText(s);
            }
        }
示例#25
0
 private static string GetCode(MethodDefinition methodDefinition)
 {
     try
     {
         var csharpLanguage       = new CSharpLanguage();
         var textOutput           = new PlainTextOutput();
         var decompilationOptions = new DecompilationOptions();
         decompilationOptions.FullDecompilation = true;
         csharpLanguage.DecompileMethod(methodDefinition, textOutput, decompilationOptions);
         return(textOutput.ToString());
     }
     catch (Exception exception)
     {
         //PublicDI.log.error("in getSourceCode: {0}", new object[] { exception.Message });
         return("Error in creating source code from IL: " + exception.Message);
     }
 }
示例#26
0
        protected override void Execute(ExceptionsCtxMenuContext context)
        {
            var output = new PlainTextOutput();

            foreach (var vm in context.SelectedItems)
            {
                var printer = new ExceptionPrinter(output);
                printer.WriteName(vm);
                output.WriteLine();
            }
            var s = output.ToString();

            if (s.Length > 0)
            {
                Clipboard.SetText(s);
            }
        }
示例#27
0
 public string getSourceCode(TypeDefinition typeDefinition)
 {
     try
     {
         var csharpLanguage       = new CSharpLanguage();
         var textOutput           = new PlainTextOutput();
         var decompilationOptions = new DecompilationOptions();
         decompilationOptions.FullDecompilation = true;
         csharpLanguage.DecompileType(typeDefinition, textOutput, decompilationOptions);
         return(textOutput.ToString());
     }
     catch (Exception exception)
     {
         PublicDI.log.error("in getSourceCode: {0}", new object[] { exception.Message });
         return("Error in creating source code from Type: " + exception.Message);
     }
 }
示例#28
0
        internal static string Decompile(PEFile assembly, IDebugInfoProvider debugInfoProvider)
        {
            var ilDecompilation = new PlainTextOutput()
            {
                IndentationString = Defaults.DefaultIndent
            };

            var ilDecompiler = new ReflectionDisassembler(ilDecompilation, CancellationToken.None)
            {
                DebugInfo = debugInfoProvider,
                DetectControlStructure  = true,
                ExpandMemberDefinitions = true,
                ShowMetadataTokens      = true,
                ShowSequencePoints      = true
            };

            ilDecompiler.WriteModuleContents(assembly);

            return(ilDecompilation.ToString());
        }
        private static string GenerateTypeDescription(ModuleDefinition module, TypeDefinition type)
        {
            var context    = new DecompilerContext(module);
            var astBuilder = new AstBuilder(context)
            {
                DecompileMethodBodies = false
            };

            astBuilder.AddType(type);

            if (!(type.IsEnum || type.IsInterface || type.IsDelegate()))
            {
                var typeNode = astBuilder.SyntaxTree.GetTypes()
                               .OfType <TypeDeclaration>().First();

                // Remove nested types
                foreach (var node in typeNode.Children)
                {
                    if (node is TypeDeclaration)
                    {
                        node.Remove();
                    }
                }

                // Remove non-public members
                foreach (var member in typeNode.Members)
                {
                    if (!(member.HasModifier(Modifiers.Public) || member.HasModifier(Modifiers.Protected)))
                    {
                        member.Remove();
                    }
                }
            }

            astBuilder.RunTransformations();
            AddXmlDocTransform.Run(astBuilder.SyntaxTree);
            var output = new PlainTextOutput();

            astBuilder.GenerateCode(output);
            return(output.ToString());
        }
示例#30
0
        private static string GetSourceCode(PropertyDefinition propertyDefinition)
        {
            try
            {
                var csharpLanguage       = new CSharpLanguage();
                var textOutput           = new PlainTextOutput();
                var decompilationOptions = new DecompilationOptions();

                decompilationOptions.DecompilerSettings.FullyQualifyAmbiguousTypeNames = true;
                decompilationOptions.DecompilerSettings.ShowXmlDocumentation           = true;
                decompilationOptions.FullDecompilation = true;

                csharpLanguage.DecompileProperty(propertyDefinition, textOutput, decompilationOptions);

                return(textOutput.ToString());
            }
            catch (Exception exception)
            {
                return("Error in creating source code from IL: " + exception.Message);
            }
        }