使用 System.Reflection 确定所有引用

发布于 2024-08-23 16:49:53 字数 333 浏览 4 评论 0原文

我试图用 System.Reflection 确定 dll 内的每个引用。但是,GetReferencedAssemblies 仅列出“参考”中的内容(在解决方案资源管理器中可见)。

我想确定代码本身内部的引用,例如 imports 语句。即使是 if/then 语句、try/catch 等,绝对一切

使用 System.Reflection 可以做到这一点吗?如果是这样,怎么办?

我肯定更愿意在没有 p/invoke 的情况下执行此操作。

感谢您的帮助!

这是vb.net中的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

‖放下 2024-08-30 16:49:53

听起来您需要 NDepend

(如果您正在编写一个工具来分析应用程序,那是一项艰巨的任务)

Sounds like you need NDepend.

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

给妤﹃绝世温柔 2024-08-30 16:49:53

编辑

正如您后面的评论所述,您只需要代码直接使用的程序集。

GetReferencedAssemblies 做到这一点。


正如您的评论所述,您正在尝试解决停止问题。

如果不实际执行原始程序集(并使用分析 API 查看它使用的类型),您就无法可靠地做到这一点。


您可以对每个引用递归调用 GetReferencedAssemblies 并获取整个间接依赖关系树。

编辑

例如:

static IEnumerable<Assembly> (Assembly a) {
    return a.GetReferencedAssemblies()
            .Concat(a.GetReferencedAssemblies()
                     .SelectMany<Assembly, Assembly>(GetAllReferences)
            );

但是,如果其中一个引用在原始程序集无法访问的代码路径中使用程序集,则可能会出现误报。由于接口和多态性,清除此类误报可能非常困难。如果任何可访问代码路径中的任何代码使用反射,则根据定义这是不可能的。

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.

鸩远一方 2024-08-30 16:49:53

您将命名空间名称与程序集名称混淆了。 System.IO 是一个命名空间。它实际上出现在不止一个程序集中。 System.IO.File类位于mscorlib.dll中,System.IO.FileSystemWatcher位于system.dll中,System.IO.Pipes.PipeStream位于system.core.dll中

GetReferencedAssemblies是准确的。

在编写此代码之前,请尝试一下 Red Gate 的 Reflector

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文