Skip to content

Instantly share code, notes, and snippets.

@ShawInnes
Created July 11, 2014 06:38
Show Gist options
  • Save ShawInnes/43f79870fed7bc51eac9 to your computer and use it in GitHub Desktop.
Save ShawInnes/43f79870fed7bc51eac9 to your computer and use it in GitHub Desktop.
Mono.Cecil Get Method Reference Count
private static int GetReferenceCount(Type type, string undesiredTypeName)
{
UriBuilder uri = new UriBuilder(type.Assembly.CodeBase);
string path = Uri.UnescapeDataString(uri.Path);
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(path);
TypeDefinition vm = assemblyDefinition.Modules.SelectMany(m => m.GetTypes().Where(p => p.FullName == type.FullName)).Single();
int referenceCount = vm.Methods.Where(p => p.HasBody)
.SelectMany(p => p.Body.Instructions.Where(i => i.OpCode.Code == Mono.Cecil.Cil.Code.Call &&
((Mono.Cecil.MethodReference)i.Operand).DeclaringType.FullName.Equals(undesiredTypeName))).Count();
return referenceCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment