当前位置: 首页>>代码示例>>C#>>正文


C# AstBuilder.AddAssembly方法代码示例

本文整理汇总了C#中ICSharpCode.Decompiler.Ast.AstBuilder.AddAssembly方法的典型用法代码示例。如果您正苦于以下问题:C# AstBuilder.AddAssembly方法的具体用法?C# AstBuilder.AddAssembly怎么用?C# AstBuilder.AddAssembly使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ICSharpCode.Decompiler.Ast.AstBuilder的用法示例。


在下文中一共展示了AstBuilder.AddAssembly方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Decompile

        public void Decompile(Stream assemblyStream, TextWriter resultWriter)
        {
            // ReSharper disable once AgentHeisenbug.CallToNonThreadSafeStaticMethodInThreadSafeType
            var module = ModuleDefinition.ReadModule(assemblyStream);
            ((BaseAssemblyResolver)module.AssemblyResolver).AddSearchDirectory(
                Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
            );

            var context = new DecompilerContext(module) {
                Settings = {
                    AnonymousMethods = false,
                    YieldReturn = false,
                    AsyncAwait = false,
                    AutomaticProperties = false,
                    ExpressionTrees = false
                }
            };

            var ast = new AstBuilder(context);
            ast.AddAssembly(module.Assembly);

            RunTransforms(ast, context);

            // I cannot use GenerateCode as it re-runs all the transforms
            var userCode = GetUserCode(ast);
            WriteResult(resultWriter, userCode, context);
        }
开发者ID:i3arnon,项目名称:TryRoslyn,代码行数:27,代码来源:AstDecompiler.cs

示例2: CompareAssemblyAgainstCSharp

        private static void CompareAssemblyAgainstCSharp(string expectedCSharpCode, string asmFilePath)
        {
            var module = Utils.OpenModule(asmFilePath);
            try
            {
                try { module.LoadPdb(); } catch { }
                AstBuilder decompiler = new AstBuilder(DecompilerContext.CreateTestContext(module));
                decompiler.AddAssembly(module, false, true, true);
                new Helpers.RemoveCompilerAttribute().Run(decompiler.SyntaxTree);
                StringWriter output = new StringWriter();

                // the F# assembly contains a namespace `<StartupCode$tmp6D55>` where the part after tmp is randomly generated.
                // remove this from the ast to simplify the diff
                var startupCodeNode = decompiler.SyntaxTree.Children.OfType<NamespaceDeclaration>().SingleOrDefault(d => d.Name.StartsWith("<StartupCode$", StringComparison.Ordinal));
                if (startupCodeNode != null)
                    startupCodeNode.Remove();

                decompiler.GenerateCode(new PlainTextOutput(output));
                var fullCSharpCode = output.ToString();

                CodeAssert.AreEqual(expectedCSharpCode, output.ToString());
            }
            finally
            {
                File.Delete(asmFilePath);
                File.Delete(Path.ChangeExtension(asmFilePath, ".pdb"));
            }
        }
开发者ID:hmemcpy,项目名称:dnSpy,代码行数:28,代码来源:TestHelpers.cs

示例3: CreateBuilder

		AstBuilder CreateBuilder(IDnlibDef item, CancellationToken token) {
			ModuleDef moduleDef;

			DecompilerContext ctx;
			AstBuilder builder;

			if (item is ModuleDef) {
				var def = (ModuleDef)item;
				moduleDef = def;
				builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
				builder.AddAssembly(def, true);
			}
			else if (item is TypeDef) {
				var def = (TypeDef)item;
				moduleDef = def.Module;
				builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
				builder.DecompileMethodBodies = false;
				ctx.CurrentType = def;
				builder.AddType(def);
			}
			else if (item is MethodDef) {
				var def = (MethodDef)item;
				moduleDef = def.Module;
				builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
				ctx.CurrentType = def.DeclaringType;
				builder.AddMethod(def);
			}
			else if (item is FieldDef) {
				var def = (FieldDef)item;
				moduleDef = def.Module;
				builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
				ctx.CurrentType = def.DeclaringType;
				builder.AddField(def);
			}
			else if (item is PropertyDef) {
				var def = (PropertyDef)item;
				moduleDef = def.Module;
				builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
				ctx.CurrentType = def.DeclaringType;
				builder.AddProperty(def);
			}
			else if (item is EventDef) {
				var def = (EventDef)item;
				moduleDef = def.Module;
				builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
				ctx.CurrentType = def.DeclaringType;
				builder.AddEvent(def);
			}
			else
				return null;

			ctx.Settings = new DecompilerSettings {
				UsingDeclarations = false
			};
			return builder;
		}
开发者ID:mamingxiu,项目名称:dnExplorer,代码行数:56,代码来源:CSharpLanguage.cs

示例4: RoundtripCode

		/// <summary>
		/// Compiles and decompiles a source code.
		/// </summary>
		/// <param name="code">The source code to copile.</param>
		/// <returns>The decompilation result of compiled source code.</returns>
		static string RoundtripCode(string code)
		{
			AssemblyDefinition assembly = Compile(code);
			AstBuilder decompiler = new AstBuilder(new DecompilerContext());
			decompiler.AddAssembly(assembly);
			decompiler.Transform(new Helpers.RemoveCompilerAttribute());
			StringWriter output = new StringWriter();
			decompiler.GenerateCode(new PlainTextOutput(output));
			return output.ToString();
		}
开发者ID:hlesesne,项目名称:ILSpy,代码行数:15,代码来源:DecompilerTestBase.cs

示例5: Run

		void Run(string compiledFile, string expectedOutputFile)
		{
			string expectedOutput = File.ReadAllText(Path.Combine(path, expectedOutputFile));
			var assembly = AssemblyDefinition.ReadAssembly(Path.Combine(path, compiledFile));
			AstBuilder decompiler = new AstBuilder(new DecompilerContext(assembly.MainModule));
			decompiler.AddAssembly(assembly);
			new Helpers.RemoveCompilerAttribute().Run(decompiler.SyntaxTree);
			StringWriter output = new StringWriter();
			decompiler.GenerateCode(new PlainTextOutput(output));
			CodeAssert.AreEqual(expectedOutput, output.ToString());
		}
开发者ID:Costo,项目名称:Xamarin.Forms,代码行数:11,代码来源:ILTests.cs

示例6: DecompileFile

        public void DecompileFile(string input, TextWriter writer)
        {
            var assembly = AssemblyDefinition.ReadAssembly(input, new ReaderParameters() {
                AssemblyResolver = new IgnoringExceptionsAssemblyResolver()
            });

            var decompiler = new AstBuilder(new DecompilerContext(assembly.MainModule));
            decompiler.AddAssembly(assembly);
            decompiler.GenerateCode(new PlainTextOutput(writer));
            writer.Close();
        }
开发者ID:Gentlee,项目名称:CSharpDecompiler,代码行数:11,代码来源:Decompiler.cs

示例7: Decompile

 public void Decompile()
 {
     AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly("Salient.JsonSchemaUtilities.dll");
     DecompilerSettings settings = new DecompilerSettings();
     settings.FullyQualifyAmbiguousTypeNames = false;
     AstBuilder decompiler = new AstBuilder(new DecompilerContext(assembly.MainModule) { Settings = settings });
     decompiler.AddAssembly(assembly);
     //new Helpers.RemoveCompilerAttribute().Run(decompiler.CompilationUnit);
     StringWriter output = new StringWriter();
     decompiler.GenerateCode(new PlainTextOutput(output));
     var code = output.ToString();
 }
开发者ID:sopel,项目名称:Salient.ReliableHttpClient,代码行数:12,代码来源:Decompiler.cs

示例8: RoundtripCode

		/// <summary>
		/// Compiles and decompiles a source code.
		/// </summary>
		/// <param name="code">The source code to copile.</param>
		/// <returns>The decompilation result of compiled source code.</returns>
		static string RoundtripCode(string code)
		{
			DecompilerSettings settings = new DecompilerSettings();
			settings.FullyQualifyAmbiguousTypeNames = false;
			AssemblyDefinition assembly = Compile(code);
			AstBuilder decompiler = new AstBuilder(new DecompilerContext(assembly.MainModule) { Settings = settings });
			decompiler.AddAssembly(assembly);
			new Helpers.RemoveCompilerAttribute().Run(decompiler.CompilationUnit);
			StringWriter output = new StringWriter();
			decompiler.GenerateCode(new PlainTextOutput(output));
			return output.ToString();
		}
开发者ID:KAW0,项目名称:Alter-Native,代码行数:17,代码来源:DecompilerTestBase.cs

示例9: AssertRoundtripCode

		protected static void AssertRoundtripCode(string fileName, bool optimize = false, bool useDebug = false, int compilerVersion = 4)
		{
			var code = RemoveIgnorableLines(File.ReadLines(fileName));
			AssemblyDef assembly = CompileLegacy(code, optimize, useDebug, compilerVersion);

			AstBuilder decompiler = new AstBuilder(DecompilerContext.CreateTestContext(assembly.ManifestModule));
			decompiler.AddAssembly(assembly);
			new Helpers.RemoveCompilerAttribute().Run(decompiler.SyntaxTree);

			StringWriter output = new StringWriter();
			decompiler.GenerateCode(new PlainTextOutput(output));
			CodeAssert.AreEqual(code, output.ToString());
		}
开发者ID:arkanoid1,项目名称:dnSpy,代码行数:13,代码来源:DecompilerTestBase.cs

示例10: TestFile

		static void TestFile(string fileName)
		{
			string code = File.ReadAllText(fileName);
			AssemblyDefinition assembly = Compile(code);
			AstBuilder decompiler = new AstBuilder(new DecompilerContext());
			decompiler.AddAssembly(assembly);
			decompiler.Transform(new Helpers.RemoveCompilerAttribute());
			StringWriter output = new StringWriter();
			decompiler.GenerateCode(new PlainTextOutput(output));
			StringWriter diff = new StringWriter();
			if (!Compare(code, output.ToString(), diff)) {
				throw new Exception("Test failure." + Environment.NewLine + diff.ToString());
			}
		}
开发者ID:hlesesne,项目名称:ILSpy,代码行数:14,代码来源:TestRunner.cs

示例11: DecompileModule

        private void DecompileModule(ClrModule module)
        {
            var assemblyDef = AssemblyDefinition.ReadAssembly(module.FileName);
            AstBuilder decompiler = new AstBuilder(
                new DecompilerContext(assemblyDef.MainModule));
            decompiler.AddAssembly(assemblyDef);

            GenerateCode(decompiler);
        }
开发者ID:goldshtn,项目名称:msos,代码行数:9,代码来源:Decompile.cs

示例12: TestFile

		static void TestFile(string fileName, bool optimize)
		{
			string code = File.ReadAllText(fileName);
			AssemblyDefinition assembly = Compile(code, optimize);
			AstBuilder decompiler = new AstBuilder(new DecompilerContext(assembly.MainModule));
			decompiler.AddAssembly(assembly);
			new Helpers.RemoveCompilerAttribute().Run(decompiler.CompilationUnit);
			StringWriter output = new StringWriter();
			decompiler.GenerateCode(new PlainTextOutput(output));
			CodeAssert.AreEqual(code, output.ToString());
		}
开发者ID:nocache,项目名称:monodevelop,代码行数:11,代码来源:TestRunner.cs

示例13: Run

        void Run(Config config)
        {
            if (config.ShowHelp) {
                Console.WriteLine ("Netjs compiler, Copyright 2014 Frank A. Krueger");
                Console.WriteLine ("netjs [options] assembly-file");
                return;
            }

            if (string.IsNullOrEmpty (config.MainAssembly)) {
                throw new Exception ("No assembly specified.");
            }

            var asmPath = Path.GetFullPath (config.MainAssembly);
            asmDir = Path.GetDirectoryName (asmPath);
            var outPath = Path.ChangeExtension (asmPath, ".ts");

            Step ("Reading IL");
            var parameters = new ReaderParameters {
                AssemblyResolver = this,
            };
            var asm = AssemblyDefinition.ReadAssembly (asmPath, parameters);
            mscorlib = AssemblyDefinition.ReadAssembly (typeof(String).Assembly.Location, parameters);
            system = AssemblyDefinition.ReadAssembly (typeof(INotifyPropertyChanged).Assembly.Location, parameters);
            systemCore = AssemblyDefinition.ReadAssembly (typeof(Enumerable).Assembly.Location, parameters);

            Step ("Decompiling IL to C#");
            var context = new DecompilerContext (asm.MainModule);
            context.Settings.ForEachStatement = false;
            context.Settings.ObjectOrCollectionInitializers = false;
            context.Settings.UsingStatement = false;
            context.Settings.AsyncAwait = false;
            context.Settings.AutomaticProperties = true;
            context.Settings.AutomaticEvents = true;
            context.Settings.QueryExpressions = false;
            context.Settings.AlwaysGenerateExceptionVariableForCatchBlocks = true;
            context.Settings.UsingDeclarations = false;
            context.Settings.FullyQualifyAmbiguousTypeNames = true;
            context.Settings.YieldReturn = false;
            var builder = new AstBuilder (context);
            builder.AddAssembly (asm);
            foreach (var a in referencedAssemblies.Values) {
                if (a != null)
                    builder.AddAssembly (a);
            }
            builder.RunTransformations ();

            Step ("Translating C# to TypeScript");
            new CsToTs ().Run (builder.SyntaxTree);

            Step ("Writing");
            using (var outputWriter = new StreamWriter (outPath)) {
                var output = new PlainTextOutput (outputWriter);
                builder.GenerateCode (output, (s, e) => new TsOutputVisitor (s, e));
            }

            Step ("Done");
        }
开发者ID:ninenine,项目名称:Netjs,代码行数:57,代码来源:App.cs

示例14: decompileAssembly

        string decompileAssembly(string path)
        {
            Step("Reading IL");
            var parameters = new ReaderParameters
            {
                AssemblyResolver = this,
            };
            var asm = AssemblyDefinition.ReadAssembly(path, parameters);
            mscorlib = AssemblyDefinition.ReadAssembly(typeof(String).Assembly.Location, parameters);
            system = AssemblyDefinition.ReadAssembly(typeof(INotifyPropertyChanged).Assembly.Location, parameters);
            systemCore = AssemblyDefinition.ReadAssembly(typeof(Enumerable).Assembly.Location, parameters);
            systemDrawing = AssemblyDefinition.ReadAssembly(typeof(System.Drawing.Bitmap).Assembly.Location, parameters);
            Step("Decompiling IL to C#");
            var context = new DecompilerContext(asm.MainModule);
            context.Settings.ForEachStatement = false;
            context.Settings.ObjectOrCollectionInitializers = false;
            context.Settings.UsingStatement = false;
            context.Settings.AsyncAwait = false;
            context.Settings.AutomaticProperties = true;
            context.Settings.AutomaticEvents = true;
            context.Settings.QueryExpressions = false;
            context.Settings.AlwaysGenerateExceptionVariableForCatchBlocks = true;
            context.Settings.UsingDeclarations = true;
            context.Settings.FullyQualifyAmbiguousTypeNames = true;
            context.Settings.YieldReturn = false;
            var builder = new AstBuilder(context);
            builder.AddAssembly(asm);

            foreach (var a in referencedAssemblies.Values)
            {
                if (a != null)
                    builder.AddAssembly(a);
            }
            /*{
                var type = asm.MainModule.Types.ElementAt(16);
                Console.WriteLine(type + "::");
                var astBuilder = new AstBuilder(new DecompilerContext(asm.MainModule) { CurrentType = type, Settings=context.Settings.Clone()});
                astBuilder.AddType(type);
                astBuilder.RunTransformations();
                var op = new PlainTextOutput();
                astBuilder.GenerateCode(op);
                Console.WriteLine(op.ToString());
            }*/
            foreach (var transform in CreatePipeline(context))
            {
                transform.Run(builder.SyntaxTree);
            }

            builder.SyntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true });

            var str = new StringWriter();
            var outputFormatter = new TextTokenWriter(new PlainTextOutput(str), context) { FoldBraces = context.Settings.FoldBraces };
            builder.SyntaxTree.AcceptVisitor(new CSharpOutputVisitor(outputFormatter, context.Settings.CSharpFormattingOptions));
            return str.GetStringBuilder().ToString();
        }
开发者ID:phiresky,项目名称:Netjs,代码行数:55,代码来源:App.cs

示例15: Run

        void Run(Config config)
        {
            if (config.AssembliesToDecompile.Count == 0) {
                config.ShowHelp = true;
            }

            if (config.ShowHelp) {
                Console.WriteLine ("Netjs compiler, Copyright 2014-2016 Frank A. Krueger");
                Console.WriteLine ("netjs [options] assembly-files");
                Console.WriteLine ("   --help, -h           Show usage information");
                Console.WriteLine ("   --includerefs, -r    Decompile referenced assemblies");
                return;
            }

            string outPath = "";
            var asmPaths = new List<string> ();

            foreach (var asmRelPath in config.AssembliesToDecompile) {
                var asmPath = Path.GetFullPath (asmRelPath);
                asmPaths.Add (asmPath);

                if (string.IsNullOrEmpty (outPath)) {
                    outPath = Path.ChangeExtension (asmPath, ".ts");
                }

                var asmDir = Path.GetDirectoryName (asmPath);
                if (!asmSearchPaths.Exists (x => x.Item1 == asmDir)) {
                    asmSearchPaths.Add (Tuple.Create (asmDir, config.IncludeRefs));
                }
            }

            Step ("Reading IL");
            globalReaderParameters.AssemblyResolver = this;
            globalReaderParameters.ReadingMode = ReadingMode.Immediate;

            var libDir = Path.GetDirectoryName (typeof (String).Assembly.Location);
            asmSearchPaths.Add (Tuple.Create(libDir, false));
            asmSearchPaths.Add (Tuple.Create(Path.Combine (libDir, "Facades"), false));

            AssemblyDefinition firstAsm = null;
            foreach (var asmPath in asmPaths) {
                var asm = AssemblyDefinition.ReadAssembly (asmPath, globalReaderParameters);
                if (firstAsm == null)
                    firstAsm = asm;
                referencedAssemblies[asm.Name.Name] = asm;
                decompileAssemblies.Add (asm);
            }

            Step ("Decompiling IL to C#");
            var context = new DecompilerContext (firstAsm.MainModule);
            context.Settings.ForEachStatement = false;
            context.Settings.ObjectOrCollectionInitializers = false;
            context.Settings.UsingStatement = false;
            context.Settings.AsyncAwait = false;
            context.Settings.AutomaticProperties = true;
            context.Settings.AutomaticEvents = true;
            context.Settings.QueryExpressions = false;
            context.Settings.AlwaysGenerateExceptionVariableForCatchBlocks = true;
            context.Settings.UsingDeclarations = false;
            context.Settings.FullyQualifyAmbiguousTypeNames = true;
            context.Settings.YieldReturn = false;
            var builder = new AstBuilder (context);
            var decompiled = new HashSet<string> ();
            for (;;) {
                var a = decompileAssemblies.FirstOrDefault (x => !decompiled.Contains (x.FullName));
                if (a != null) {
                    Info ("  Decompiling {0}", a.FullName);
                    builder.AddAssembly (a);
                    decompiled.Add (a.FullName);
                }
                else {
                    break;
                }
            }
            builder.RunTransformations ();

            Step ("Translating C# to TypeScript");
            new CsToTs ().Run (builder.SyntaxTree);

            Step ("Writing");
            using (var outputWriter = new StreamWriter (outPath)) {
                var output = new PlainTextOutput (outputWriter);
                builder.GenerateCode (output, (s, e) => new TsOutputVisitor (s, e));
            }

            Step ("Done");
        }
开发者ID:praeclarum,项目名称:Netjs,代码行数:87,代码来源:App.cs


注:本文中的ICSharpCode.Decompiler.Ast.AstBuilder.AddAssembly方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。