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


C# Ast.AstBuilder类代码示例

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


AstBuilder类属于ICSharpCode.Decompiler.Ast命名空间,在下文中一共展示了AstBuilder类的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: 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: 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

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

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

示例6: GetUserCode

        private IEnumerable<AstNode> GetUserCode(AstBuilder ast)
        {
            //if (!scriptMode)
                return new[] { ast.SyntaxTree };

            //var scriptClass = ast.CompilationUnit.Descendants.OfType<TypeDeclaration>().First(t => t.Name == "Script");
            //return FlattenScript(scriptClass);
        }
开发者ID:i3arnon,项目名称:TryRoslyn,代码行数:8,代码来源:AstDecompiler.cs

示例7: MethodBuilder

 internal MethodBuilder(MethodDefinition methodDefinition, ModuleDefinition currentModule) {
     _methodDefinition = methodDefinition;
     _builder = new AstDecompiler.AstBuilder(new DecompilerContext(currentModule) {
         CurrentMethod = methodDefinition,
         CurrentType = methodDefinition.DeclaringType,
         Settings = new DecompilerSettings()
     });
 }
开发者ID:sagifogel,项目名称:NJection.LambdaConverter,代码行数:8,代码来源:MethodBuilder.cs

示例8: RunTransforms

        //private IEnumerable<AstNode> FlattenScript(TypeDeclaration scriptClass) {
        //    foreach (var member in scriptClass.Members) {
        //        var constructor = member as ConstructorDeclaration;
        //        if (constructor != null) {
        //            foreach (var statement in constructor.Body.Statements) {
        //                yield return statement;
        //            }
        //        }
        //        else {
        //            yield return member;
        //        }
        //    }
        //}
        private void RunTransforms(AstBuilder ast, DecompilerContext context)
        {
            var transforms = TransformationPipeline.CreatePipeline(context).ToList();
            transforms[transforms.FindIndex(t => t is ConvertConstructorCallIntoInitializer)] = new RoslynFriendlyConvertConstructorCallIntoInitializer();

            foreach (var transform in transforms) {
                transform.Run(ast.SyntaxTree);
            }
        }
开发者ID:i3arnon,项目名称:TryRoslyn,代码行数:22,代码来源:AstDecompiler.cs

示例9: AnalysisLayer

 public AnalysisLayer(StorageLayer layer)
 {
     AstBuilder astBuilder;
     var method = DecompileUtil.GetMethodCode(layer.Algorithm.GetType(), out astBuilder, "ProcessCell");
     this.Name = layer.Algorithm.GetType().Name;
     this.Code = method.Body.GetTrackedText();
     this.Algorithm = layer.Algorithm;
     this.AstBuilder = astBuilder;
 }
开发者ID:TreeSeed,项目名称:Tychaia,代码行数:9,代码来源:AnalysisLayer.cs

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

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

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

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

示例14: InnerGenerateCode

 protected override void InnerGenerateCode(AstBuilder astBuilder, ITextOutput output)
 {
     if (output is StringBuilderTextOutput)
     {
         GenerateAstJson(astBuilder, output);
     }
     else
     {
         base.InnerGenerateCode(astBuilder, output);
     }
 }
开发者ID:CompilerKit,项目名称:CodeWalk,代码行数:11,代码来源:MyCsLang.cs

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


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