Skip to content

Instantly share code, notes, and snippets.

@awithy
Created February 5, 2012 15:21
Show Gist options
  • Save awithy/1746060 to your computer and use it in GitHub Desktop.
Save awithy/1746060 to your computer and use it in GitHub Desktop.
public class CecilAssembly : IILAssembly
{
private readonly AssemblyDefinition _assemblyDefinition;
private readonly string _assemblyPath;
public CecilAssembly(string assemblyPath)
{
_assemblyPath = assemblyPath;
_assemblyDefinition = AssemblyDefinition.ReadAssembly(assemblyPath);
}
public IEnumerable<IILMethod> FindAllNonCtorMethodsWithPrefix(string prefix)
{
var typeDefinitions = _assemblyDefinition.MainModule.Types;
var typesInNamespaceToUpdate = typeDefinitions.Where(x => x.FullName.StartsWith(prefix));
var typeMethodsToUpdate = typesInNamespaceToUpdate.SelectMany(x => x.Methods).Where(x => x.Name != ".ctor");
return typeMethodsToUpdate.Select(x => new CecilMethod(x));
}
public IILMethod FindMethod(string methodName)
{
var methodDefinition =
_assemblyDefinition.MainModule.Types.SelectMany(x => x.Methods).Where(
x => x.DeclaringType + "." + x.Name == methodName).Single();
return new CecilMethod(methodDefinition);
}
public void SaveToDisk()
{
_assemblyDefinition.Write(_assemblyPath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment