I am developing a tool for Impact Analysis. If I am changing a method say "myMethod" in myAssembly, I need to get all other assemblies referencing myAssembly and I got it through below code.
Assembly a = Assembly.LoadFrom("otherAssembly");
//check if the my Assembly is referenced or not?
if (a.GetReferencedAssemblies().Where(item => item.Name == "myAssembly").LongCount() > 0)
{
//Do something
}
But here How can I know if "myMethod" of "myAssembly" is called/used or not? Otherwise I will get all other assemblies which are referencing myAssembly (say 10 in number) but out of 10 just 1 is called/used myMethod and will be impacted.
Any help will be appreciated
You need to analyze all code in those assemblies. You can use Mono.Cecil for this task.
Some more info:
What you are trying to do isn't exactly trivial. You need to iterate over all possibly executable code and examine the CIL instructions and analyze them to see whether your method is called.
As a starting point, you can get the instructions of all methods like this:
var assemblyResolver = new DefaultAssemblyResolver();
assemblyResolver.AddSearchDirectory(...);
var assemblyDefinition = assemblyResolver.Resolve(
AssemblyNameReference.Parse(fullName));
foreach(ModuleDefinition module in assemblyDefinition)
{
foreach(TypeDefinition type in module.Types)
{
foreach(MethodDefinition method in type.Methods)
{
foreach(Instruction instruction in method.Body.Instructions)
{
// Analyze it - the hard part ;-)
}
}
}
}