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