本文整理汇总了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();
}
示例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"));
}
}
示例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);
}
示例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;
}
});
}
示例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;
}
}
示例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);
}
示例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()
});
}
示例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);
}
}
示例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;
}
示例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;
}
示例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();
}
示例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());
}
示例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();
}
示例14: InnerGenerateCode
protected override void InnerGenerateCode(AstBuilder astBuilder, ITextOutput output)
{
if (output is StringBuilderTextOutput)
{
GenerateAstJson(astBuilder, output);
}
else
{
base.InnerGenerateCode(astBuilder, output);
}
}
示例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();
}