Esempio n. 1
0
        /// <summary>
        /// Asserts that the emitted IL for a type is the same as the expected IL.
        /// Many core library types are in different assemblies on .Net Framework, and .Net Core.
        /// Therefore this test is likely to fail unless you  only run it only only on one of these frameworks,
        /// or you run it on both, but provide a different expected output string for each.
        /// See <see cref="ExecutionConditionUtil"/>.
        /// </summary>
        /// <param name="typeName">The non-fully-qualified name of the type</param>
        /// <param name="expected">The expected IL</param>
        public void VerifyTypeIL(string typeName, string expected)
        {
            var output = new ICSharpCode.Decompiler.PlainTextOutput();

            using (var testEnvironment = RuntimeEnvironmentFactory.Create(_dependencies))
            {
                string             mainModuleFullName = Emit(testEnvironment, manifestResources: null, EmitOptions.Default);
                IList <ModuleData> moduleData         = testEnvironment.GetAllModuleData();
                var mainModule = moduleData.Single(md => md.FullName == mainModuleFullName);
                using (var moduleMetadata = ModuleMetadata.CreateFromImage(testEnvironment.GetMainImage()))
                {
                    var peFile         = new PEFile(mainModuleFullName, moduleMetadata.Module.PEReaderOpt);
                    var metadataReader = moduleMetadata.GetMetadataReader();

                    bool found = false;
                    foreach (var typeDefHandle in metadataReader.TypeDefinitions)
                    {
                        var typeDef = metadataReader.GetTypeDefinition(typeDefHandle);
                        if (metadataReader.GetString(typeDef.Name) == typeName)
                        {
                            var disassembler = new ICSharpCode.Decompiler.Disassembler.ReflectionDisassembler(output, default);
                            disassembler.DisassembleType(peFile, typeDefHandle);
                            found = true;
                            break;
                        }
                    }
                    Assert.True(found, "Could not find type named " + typeName);
                }
            }
            AssertEx.AssertEqualToleratingWhitespaceDifferences(expected, output.ToString(), escapeQuotes: false);
        }
		public static string PrintIL(this MethodDefinition self)
		{
			try {
				var textoutput = new ICSharpCode.Decompiler.PlainTextOutput();
				var disasm = new ICSharpCode.Decompiler.Disassembler.ReflectionDisassembler(textoutput, true, new System.Threading.CancellationToken());
				disasm.DisassembleMethod(self);
				return textoutput.ToString();
			} catch (Exception ex) {
				MainClass.LogException(ex);
				return ex.ToString();
			}
		}
Esempio n. 3
0
 public static string PrintCSharp(this MethodDefinition self)
 {
     try {
         var textoutput = new ICSharpCode.Decompiler.PlainTextOutput();
         var options    = new ICSharpCode.ILSpy.DecompilationOptions();
         var lang       = new ICSharpCode.ILSpy.CSharpLanguage();
         lang.DecompileMethod(self, textoutput, options);
         return(textoutput.ToString());
     } catch (Exception ex) {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine(ex.Message);
         Console.WriteLine(ex.StackTrace);
         Console.ForegroundColor = ConsoleColor.Gray;
         return(ex.Message);
     }
 }
 public static string PrintCSharp(this MethodDefinition self)
 {
     try {
         var textoutput = new ICSharpCode.Decompiler.PlainTextOutput();
         var options = new ICSharpCode.ILSpy.DecompilationOptions();
         var lang = new ICSharpCode.ILSpy.CSharpLanguage();
         lang.DecompileMethod(self, textoutput, options);
         return textoutput.ToString();
     } catch (Exception ex) {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine(ex.Message);
         Console.WriteLine(ex.StackTrace);
         Console.ForegroundColor = ConsoleColor.Gray;
         return ex.Message;
     }
 }
		public static string PrintCSharp(this MethodDefinition self)
		{
			try {
				var textoutput = new ICSharpCode.Decompiler.PlainTextOutput();
				var builder = new ICSharpCode.Decompiler.Ast.AstBuilder(new ICSharpCode.Decompiler.DecompilerContext(self.DeclaringType.Module) {
					CancellationToken = new System.Threading.CancellationToken(),
					CurrentType = self.DeclaringType,
					Settings = new ICSharpCode.Decompiler.DecompilerSettings()
				});
				builder.AddMethod(self);
				builder.GenerateCode(textoutput);
				return textoutput.ToString();
			} catch (Exception ex) {
				MainClass.LogException(ex);
				return ex.ToString();
			}
		}
Esempio n. 6
0
        private static string GetCSharpCode(object source)
        {
            var output         = new ICSharpCode.Decompiler.PlainTextOutput();
            var language       = Languages.GetLanguage("C#");
            var shortDecompile = new DecompilationOptions {
                FullDecompilation = false
            };
            var fullDecompile = new DecompilationOptions {
                FullDecompilation = true
            };

            var assembly = source as AssemblyDefinition;

            if (assembly != null)
            {
                language.DecompileAssembly(assembly, "", output, shortDecompile);
                return(output.ToString());
            }

            var ns = source as NamespaceDefinition;

            if (ns != null)
            {
                //writer.WriteNamespace(ns, output, null);
                //return output.ToString();
                return("NOT IMPLEMENTED YET");
            }

            var module = source as ModuleDefinition;

            if (module != null)
            {
                //writer.WriteModule(module, output, null);
                //return output.ToString();
                return("NOT IMPLEMENTED YET");
            }

            var type = source as TypeDefinition;

            if (type != null)
            {
                language.DecompileType(type, output, fullDecompile);
                return(output.ToString());
            }

            var method = source as MethodDefinition;

            if (method != null)
            {
                language.DecompileMethod(method, output, fullDecompile);
                return(output.ToString());
            }

            var property = source as PropertyDefinition;

            if (property != null)
            {
                language.DecompileProperty(property, output, fullDecompile);
                return(output.ToString());
            }

            var field = source as FieldDefinition;

            if (field != null)
            {
                language.DecompileField(field, output, fullDecompile);
                return(output.ToString());
            }

            var @event = source as EventDefinition;

            if (@event != null)
            {
                language.DecompileEvent(@event, output, fullDecompile);
                return(output.ToString());
            }

            return("NOT IMPLEMENTED YET");
        }
Esempio n. 7
0
    private static string GetCSharpCode(object source)
    {
      var output = new ICSharpCode.Decompiler.PlainTextOutput();
      var language = Languages.GetLanguage("C#");
      var shortDecompile = new DecompilationOptions { FullDecompilation = false };
      var fullDecompile = new DecompilationOptions { FullDecompilation = true };

      var assembly = source as AssemblyDefinition;
      if (assembly != null)
      {
        language.DecompileAssembly(assembly, "", output, shortDecompile);
        return output.ToString();
      }

      var ns = source as NamespaceDefinition;
      if (ns != null)
      {
        //writer.WriteNamespace(ns, output, null);
        //return output.ToString();
        return "NOT IMPLEMENTED YET";
      }

      var module = source as ModuleDefinition;
      if (module != null)
      {
        //writer.WriteModule(module, output, null);
        //return output.ToString();
        return "NOT IMPLEMENTED YET";
      }

      var type = source as TypeDefinition;
      if (type != null)
      {
        language.DecompileType(type, output, fullDecompile);        
        return output.ToString();
      }

      var method = source as MethodDefinition;
      if (method != null)
      {
        language.DecompileMethod(method, output, fullDecompile);
        return output.ToString();
      }

      var property = source as PropertyDefinition;
      if (property != null)
      {
        language.DecompileProperty(property, output, fullDecompile);
        return output.ToString();
      }

      var field = source as FieldDefinition;
      if (field != null)
      {
        language.DecompileField(field, output, fullDecompile);
        return output.ToString();
      }

      var @event = source as EventDefinition;
      if (@event != null)
      {
        language.DecompileEvent(@event, output, fullDecompile);
        return output.ToString();
      }

      return "NOT IMPLEMENTED YET";
    }
Esempio n. 8
-2
        static void Main(string[] args)
        {
            string assemblyPath = @"C:\build\main\i386\debug\bin\TradingTechnologies.TTAPI.dll";
            string outputPath = @"C:\build\main\i386\debug\bin\TradingTechnologies.TTAPI.dll.public_interface.txt";

            var decompilationOptions = new ICSharpCode.ILSpy.DecompilationOptions()
            {
                DecompilerSettings = new ICSharpCode.Decompiler.DecompilerSettings()
                {
                    ShowXmlDocumentation = false,
                    FullyQualifyAmbiguousTypeNames = true,
                    OnlyPublicAndProtected = true,
                }
            };

            var assemblyResolver = new MyAssemblyResolver(assemblyPath);
            var asm = assemblyResolver.ReadAssembly(assemblyPath);

            var csharpLang = new ICSharpCode.ILSpy.CSharpLanguage();

            using(var writer = new System.IO.StreamWriter(outputPath))
            {
                var output = new ICSharpCode.Decompiler.PlainTextOutput(writer);

                foreach (var module in asm.Modules.OrderBy(m => m.Name))
                {
                    foreach (var type in module.Types.Where(t => isValidNamespace(t) && t.IsPublic))
                    {
                        output.WriteLine();
                        output.Write("//====================================================================");
                        output.WriteLine();
                        output.Write("// ");
                        output.Write(type.FullName);
                        output.WriteLine();
                        output.WriteLine();

                        csharpLang.DecompileType(type, output, decompilationOptions);
                        output.WriteLine();
                    }
                }

                
                //csharpLang.DecompileAssembly(asm, output, decompilationOptions);
            }
        }