本文整理汇总了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();
}
示例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;
}
}
示例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;
}
});
}
示例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"));
}
}
示例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();
}
示例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());
}
示例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();
}
示例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();
}
示例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();
}
示例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());
}
示例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());
}
}
示例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();
}
示例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;
}
示例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;
}
示例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);
}