문제

I am trying to determine 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.

도움이 되었습니까?

해결책

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.

다른 팁

Sounds like you need NDepend.

(If you are writing a tool to analyze an app that's a big task)

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top