Skip to content

Instantly share code, notes, and snippets.

@alexandrnikitin
Created May 16, 2016 09:28
Show Gist options
  • Save alexandrnikitin/b1b09bc932d2538f1dbb97ba32e90346 to your computer and use it in GitHub Desktop.
Save alexandrnikitin/b1b09bc932d2538f1dbb97ba32e90346 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.IO;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
namespace Analysis
{
internal class Program
{
public static void Main(string[] args)
{
var folderPath = @"c:\temp\";
using (
StreamWriter outfile =
new StreamWriter(@"c:\temp\newEmptyArr.txt"))
{
int i = 0;
foreach (var dll in GetAssemblies(folderPath))
{
ModuleDefinition module;
if (!TryGetModule(dll, out module))
{
continue;
}
foreach (var currentType in module.GetTypes())
{
foreach (var currentMethod in currentType.GetMethods())
{
if (currentMethod.HasBody)
{
foreach (var currentInstruction in currentMethod.Body.Instructions)
{
if (currentInstruction.OpCode != OpCodes.Ldc_I4_0)
{
continue;
}
var nextInstruction = currentInstruction.Next;
if (nextInstruction.OpCode != OpCodes.Newarr)
{
continue;
}
i++;
outfile.WriteLine("--------------------------------------------------------");
outfile.WriteLine("Item #{0}", i);
outfile.WriteLine("Type name: {0}", currentType);
outfile.WriteLine("Method full name: {0}", currentMethod.FullName);
outfile.WriteLine("Instructions:");
outfile.WriteLine(currentInstruction);
outfile.WriteLine(nextInstruction);
outfile.WriteLine();
outfile.WriteLine();
}
}
}
}
}
outfile.WriteLine("Total items found: {0}", i);
}
}
private static IEnumerable<string> GetAssemblies(string folderPath)
{
return Directory.EnumerateFiles(folderPath, "*.dll");
}
private static bool TryGetModule(string pathToAssembly, out ModuleDefinition moduleDefinition)
{
try
{
moduleDefinition = ModuleDefinition.ReadModule(pathToAssembly);
return true;
}
catch
{
moduleDefinition = default(ModuleDefinition);
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment