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


C# AstBuilder.GenerateCode方法代码示例

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


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

示例1: GetClass

        public string GetClass(TypeIdentity identity)
        {
            // Before we attempt to fetch it just try a decompilation.
            GetAssembly(identity.AssemblyPath);

            ModuleDefinition moduleDef;
            if (!this.loadedModules.TryGetValue(identity.AssemblyPath, out moduleDef))
            {
                // Can't find the assembly, just return nothing.
                return string.Empty;
            }

            TypeDefinition typeDef = moduleDef.GetType(identity.FullyQualifiedName);
            if (typeDef == null)
            {
                // If we can't find our type just return as well.
                return string.Empty;
            }

            DecompilerContext context = new DecompilerContext(moduleDef);
            AstBuilder astBuilder = new AstBuilder(context);
            astBuilder.AddType(typeDef);

            PlainTextOutput textOutput = new PlainTextOutput();
            astBuilder.GenerateCode(textOutput);
            return textOutput.ToString();
        }
开发者ID:chemix-lunacy,项目名称:Skyling,代码行数:27,代码来源:AssemblyAnalyzer.cs

示例2: DecompileOnDemand

		public void DecompileOnDemand(TypeDefinition type)
		{
			if (type == null)
				return;
			
			if (CheckMappings(type.MetadataToken.ToInt32()))
				return;
			
			try {
				DecompilerContext context = new DecompilerContext(type.Module);
				AstBuilder astBuilder = new AstBuilder(context);
				astBuilder.AddType(type);
				astBuilder.GenerateCode(new PlainTextOutput());
				
				int token = type.MetadataToken.ToInt32();
				var info = new DecompileInformation {
					CodeMappings = astBuilder.CodeMappings,
					LocalVariables = astBuilder.LocalVariables,
					DecompiledMemberReferences = astBuilder.DecompiledMemberReferences
				};
				
				// save the data
				DebugInformation.AddOrUpdate(token, info, (k, v) => info);
			} catch {
				return;
			}
		}
开发者ID:pir0,项目名称:SharpDevelop,代码行数:27,代码来源:DebuggerDecompilerService.cs

示例3: GetSourceCode

 public static async Task<string> GetSourceCode(MethodDefinition methodDefinition, ILWeaver weaver = null)
 {
     return await Task.Run(() =>
     {
         try
         {
             if (weaver != null) weaver.Apply(methodDefinition.Body);
             var settings = new DecompilerSettings { UsingDeclarations = false };
             var context = new DecompilerContext(methodDefinition.Module)
             {
                 CurrentType = methodDefinition.DeclaringType,
                 Settings = settings
             };
             var astBuilder = new AstBuilder(context);
             astBuilder.AddMethod(methodDefinition);
             var textOutput = new PlainTextOutput();
             astBuilder.GenerateCode(textOutput);
             return textOutput.ToString();
         }
         catch (Exception ex)
         {
             return "Error in creating source code from IL: " + ex.Message + Environment.NewLine + ex.StackTrace;
         }
         finally
         {
             if (weaver != null) methodDefinition.Body = null;
         }
     });
 }
开发者ID:AEtherSurfer,项目名称:OxidePatcher,代码行数:29,代码来源:Decompiler.cs

示例4: 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

示例5: 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

示例6: 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

示例7: 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

示例8: 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

示例9: 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

示例10: 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

示例11: 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

示例12: ToSource

 public static string ToSource(MethodDefinition methodDefinition)
 {
     var settings = new DecompilerSettings { UsingDeclarations = false };
     var context = new DecompilerContext(methodDefinition.Module)
     {
         CurrentType = methodDefinition.DeclaringType,
         Settings = settings,
     };
     var astBuilder = new AstBuilder(context);
     astBuilder.AddMethod(methodDefinition);
     var textOutput = new PlainTextOutput();
     astBuilder.GenerateCode(textOutput);
     return textOutput.ToString();
 }
开发者ID:JamesWilko,项目名称:UnityExtensionPatcher,代码行数:14,代码来源:Decompiler.cs

示例13: Decompile

		private static string Decompile(string name, MethodDefinition mtd) {
			
			var decompilerSettings = new ICSharpCode.Decompiler.DecompilerSettings {
				ShowXmlDocumentation = false,
				UsingDeclarations = false,
			};
			var output = new ICSharpCode.Decompiler.PlainTextOutput();
			var method = mtd;
			var astBuilder = new AstBuilder(new DecompilerContext(method.DeclaringType.Module) {
				CancellationToken = new CancellationToken(),
				CurrentType = method.DeclaringType,
				Settings = decompilerSettings,
			});

			astBuilder.AddMethod(method);
			astBuilder.GenerateCode(output);
			var methodCode = output.ToString();

			// remove top comment line
			//if (methodCode.StartsWith("//")) {
			//	methodCode = methodCode.Substring(methodCode.IndexOf('\n') + 1);
			//}


		    var attrRE = new Regex(@"^(?:\[[^]]+]\s*){1,}");
			methodCode = attrRE.Replace(methodCode, "", 1);

			// change the method name to the mod's name for the method, and replace parameter names with game names
			var methodName = mtd.Name;
			var nameLocation = methodCode.IndexOf(" " + methodName) + 1;
			var nameEnd = nameLocation + methodName.Length;

			// Prepend "void " if this was a constructor (since methodCode won't have a return type)
			var correctName = mtd.IsConstructor ? ("void " + name) : name;
			methodCode = methodCode.Substring(0, nameLocation) + correctName + methodCode.Substring(nameEnd);
			return methodCode;
		}
开发者ID:Empyrial,项目名称:IEMod.pw,代码行数:37,代码来源:ModifiedDecompiler.cs

示例14: Decompile

		public static List<ReferenceSegment> Decompile (TextEditorData data, ModuleDefinition module, TypeDefinition currentType, Action<AstBuilder> setData)
		{
			try {
				var types = DesktopService.GetMimeTypeInheritanceChain (data.Document.MimeType);
				var codePolicy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy> (types);
				
				var context = new DecompilerContext (module);
				var source = new CancellationTokenSource ();
				
				context.CancellationToken = source.Token;
				context.CurrentType = currentType;
				
				context.Settings = new DecompilerSettings () {
					AnonymousMethods = true,
					AutomaticEvents  = true,
					AutomaticProperties = true,
					ForEachStatement = true,
					LockStatement = true
				};
				
				AstBuilder astBuilder = new AstBuilder (context);
				
				setData (astBuilder);
				
				astBuilder.RunTransformations (o => false);
				var output = new ColoredCSharpFormatter (data.Document);
				astBuilder.GenerateCode (output, codePolicy.CreateOptions ());
				output.SetDocumentData ();
				return output.ReferencedSegments;
			} catch (Exception e) {
				data.Text = "Decompilation failed: \n" + e;
			}
			return null;
		}
开发者ID:yayanyang,项目名称:monodevelop,代码行数:34,代码来源:DomMethodNodeBuilder.cs

示例15: RunTransformsAndGenerateCode

 void RunTransformsAndGenerateCode(AstBuilder astBuilder, ITextOutput output, DecompilationOptions options, IAstTransform additionalTransform = null)
 {
     astBuilder.RunTransformations(transformAbortCondition);
     if (additionalTransform != null) {
         additionalTransform.Run(astBuilder.SyntaxTree);
     }
     if (options.DecompilerSettings.ShowXmlDocumentation) {
         try {
             AddXmlDocTransform.Run(astBuilder.SyntaxTree);
         } catch (XmlException ex) {
             string[] msg = (" Exception while reading XmlDoc: " + ex.ToString()).Split(new[]{'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
             var insertionPoint = astBuilder.SyntaxTree.FirstChild;
             for (int i = 0; i < msg.Length; i++)
                 astBuilder.SyntaxTree.InsertChildBefore(insertionPoint, new Comment(msg[i], CommentType.Documentation), Roles.Comment);
         }
     }
     astBuilder.GenerateCode(output);
 }
开发者ID:PoroCYon,项目名称:ILSpy,代码行数:18,代码来源:CSharpLanguage.cs


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