Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

I'm trying develop an application that read all referenced dll for one other dll. Through these references, I get all methods used. But I don't want get all existing methods. I want get only really used methods, ie, only the methods called by the dll being read. Is there any way to do this using Reflection?

Excuse-me because my english is not good.

Thanks
Posted
Comments
Albin Abel 2-Mar-11 13:42pm    
That you have to look at your assembly, not in the dlls. Look at your assembly where ever those methods are called
Sergey Alexandrovich Kryukov 2-Mar-11 14:33pm    
Let's make no difference between different kinds of DLLs.
--SA
Sergey Alexandrovich Kryukov 2-Mar-11 14:34pm    
This is a good difficult question my 5!
Do you assume that source code is not available? It it is, the problem is easy.
--SA
borodai 2-Mar-11 14:36pm    
Actually, I'm trying to list all the dependencies for a dll. In the Import Table, I have information from all references to a dll. It turns out that these
references do not show specifically which methods are called. Besides knowing what are dll references, want to know what methods are used that dll refences.

Here is the code I'm using:

Collapse

Hashtable alreadyLoaded = new Hashtable();
foreach (string name in dllsVerDependem)
{
Assembly assm = Assembly.LoadFrom(name);
DumpAssembly1(assm, alreadyLoaded, 0, DllProcurada, name);
}


static void DumpAssembly(Assembly assm, Hashtable alreadyLoaded, int indent)
{
Console.Write(new String(' ', indent));
AssemblyName fqn = assm.GetName();
if (alreadyLoaded.Contains(fqn.FullName))
{
Console.WriteLine("[{0}]", fqn.Name);
return;
}
alreadyLoaded[fqn.FullName] = fqn.FullName;
Console.WriteLine(fqn.Name);
foreach (AssemblyName name in assm.GetReferencedAssemblies())
{
Assembly referenced = Assembly.Load(name);
DumpAssembly(referenced, alreadyLoaded, indent + 2);
}
}

I hope I have explained it better.Thanks.
borodai 2-Mar-11 14:48pm    
ok, obter as depencias é facil. Mas como saber quais métodos geram a dependência?

Se existe uma dependência, significa que algum método externo é chamado. Como saber qual método é esse?

Actually, I'm trying to list all the dependencies for a dll. In the Import Table, I have information from all references to a dll. It turns out that these
references do not show specifically which methods are called. Besides knowing what are dll references, want to know what methods are used that dll refences.

Here is the code I'm using:

Hashtable alreadyLoaded = new Hashtable();
foreach (string name in dllsVerDependem)
{
   Assembly assm = Assembly.LoadFrom(name);            
   DumpAssembly1(assm, alreadyLoaded, 0, DllProcurada, name);            
}


static void DumpAssembly(Assembly assm, Hashtable alreadyLoaded, int indent)
{
  Console.Write(new String(' ', indent));
  AssemblyName fqn = assm.GetName();
  if (alreadyLoaded.Contains(fqn.FullName))
  {
    Console.WriteLine("[{0}]", fqn.Name);
    return;
  }
  alreadyLoaded[fqn.FullName] = fqn.FullName;
  Console.WriteLine(fqn.Name);
  foreach (AssemblyName name in assm.GetReferencedAssemblies())
  {
    Assembly referenced = Assembly.Load(name);
    DumpAssembly(referenced, alreadyLoaded, indent + 2);
  }
}


I hope I have explained it better.Thaks.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Mar-11 14:36pm    
Not bad, but the question is about methods, not assembly dependencies.
Getting static assembly dependencies is way too easy...
--SA
I may not have understood your question correctly but I do not see the point of what you are trying to do.

When a dll is loaded, all of it is loaded, not just the members that are used.

Or have I misunderstood?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Mar-11 14:32pm    
If makes perfect sense, Henry. How to sort out methods that will never be used based on static code analysis. Not an easy task, by the way.
By the way you ought to know that the unused methods won't be even JIT compiled.
--SA
Sergey Alexandrovich Kryukov 2-Mar-11 15:18pm    
I made some answer, please see. Cannot offer anything better; I don't think better solution exists.
--SA
Thank you for interesting Question!

As I say, it's easy if you have the source code (Visual Studio can do it automatically). If not, I know only one method of doing it: disassemble all referencing assemblies in question and analyze the result of it. You can always use disassembler programmatically, all you need is the re-distributed .NET Framework.

(You should not use term "DLL". In contrast to native Windows, all .NET assemblies are essentially equal. You can reference *.exe in exact same way as *.DLL, did you know that?)

You should understand that you need to analyze referencing assemblies. The result on what is used on a referenced assembly depends on which assemblies use it. You should also understand the difference between methods that are statically known to be never called because there is no a calling code in the referencing assemblies. There are methods which can be called in principle (calling code exists) but are not called by the dynamic behavior of the running code. It easy to proof that sorting out such methods is theoretically unresolvable problem. The answer can be inferred from the well-know halting problem: http://en.wikipedia.org/wiki/Halting_problem[^], see also http://en.wikipedia.org/wiki/Computability_theory[^].

So, let's focus on the first, easier problem (static resolution).

As a side note, you should understand that method are JIT compiled only on demand. It means that even if you capture JIT compilation, it won't help you to solve the problem.

Unfortunately, to best of my knowledge, solving this problem using Reflection is not possible. Resolving assembly dependencies is way too, easy, you probably know that. That said, I'm not 100% sure that there is not solution. If you come across the way to solve the problem, please share.

Thank you.

—SA
 
Share this answer
 
Comments
Henry Minute 2-Mar-11 15:24pm    
As you say, dependencies are easy (relatively) back in the .NET 1.0 days MS shipped the code for a primitive dependency checker with the Framework.

I wouldn't have a clue about getting the used methods after that though.
Sergey Alexandrovich Kryukov 2-Mar-11 17:36pm    
Well, as I say, the first method -- disassembler this thing will certainly work.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900