Using System.Reflection to determine ALL references
I am trying to d开发者_高级运维etermine every single reference inside a dll with System.Reflection
. However, GetReferencedAssemblies
only lists the ones in the "References" (visible in solution explorer).
I would like to determine references from within the code itself, such as an imports
statement. Even things like if/then statements, try/catch, absolutely everything.
Is this possible to do using System.Reflection
? If so, how?
I would definitely prefer to do this without p/invoke.
Thanks for the help!
This is in vb.net.
Sounds like you need NDepend.
(If you are writing a tool to analyze an app that's a big task)
EDIT
As described by your later comment, you only want the assemblies that your code uses directly.
GetReferencedAssemblies
will do that.
As described by your comments, you're trying to solve the halting problem.
You cannot reliably do this without actually executing the original assembly (and using the profiling API to see what type it uses).
You could recursively call GetReferencedAssemblies
on each reference and get the entire tree of indirect dependencies.
EDIT:
For example:
static IEnumerable<Assembly> (Assembly a) {
return a.GetReferencedAssemblies()
.Concat(a.GetReferencedAssemblies()
.SelectMany<Assembly, Assembly>(GetAllReferences)
);
However, you can get false positives if one of the references uses an assembly in a code path that isn't reachable by the original assembly. Due to interfaces and polymorphism, it can be very difficult to weed out such false positives. If any of code in any reachable code path uses reflection, it is impossible by definition.
You are confusing namespace names with assembly names. System.IO is a namespace. It actually appears in more than one assembly. The System.IO.File class lives in mscorlib.dll, System.IO.FileSystemWatcher in system.dll, System.IO.Pipes.PipeStream in system.core.dll
GetReferencedAssemblies is accurate.
Play around with Red Gate's Reflector before writing this code.
精彩评论