【问题标题】:How do I list all loaded assemblies?如何列出所有加载的程序集?
【发布时间】:2010-10-02 06:47:34
【问题描述】:

在 .Net 中,我想枚举所有 AppDomain 上的所有加载程序集。为我的程序的 AppDomain 做这件事很容易AppDomain.CurrentDomain.GetAssemblies()。我是否需要以某种方式访问​​每个 AppDomain?还是已经有工具可以做到这一点?

【问题讨论】:

标签: c# .net assemblies


【解决方案1】:

使用 Visual Studio

  1. 将调试器附加到进程(例如,从调试开始或“调试”>“附加到进程”)
  2. 调试时,显示模块窗口(调试 > 窗口 > 模块)

这提供了有关每个程序集、应用程序域的详细信息,并提供了一些加载符号的选项(即包含调试信息的 pdb 文件)。

使用进程浏览器

如果您需要外部工具,可以使用Process Explorer(免费软件,由 Microsoft 发布)

单击一个进程,它将显示一个包含所有使用的程序集的列表。该工具非常好,因为它显示了其他信息,例如文件句柄等。

以编程方式

查看this SO 问题,该问题解释了如何操作。

【讨论】:

  • 这比这里解释的还要好,因为在进程的属性页面中,Process Explorer 会准确显示加载了哪些 AppDomain(包括“共享域”)程序集。因此,它不仅显示了将哪些 .dll 加载到进程中。很高兴知道他们使用哪些 API 来显示这一点(关于“以编程方式”链接只会给出 CurrentDomain 中的程序集)。
【解决方案2】:

这就是我最终的结果。它是所有属性和方法的列表,我列出了每个方法的所有参数。我没有成功获取所有值。

foreach(System.Reflection.AssemblyName an in System.Reflection.Assembly.GetExecutingAssembly().GetReferencedAssemblies()){                      
            System.Reflection.Assembly asm = System.Reflection.Assembly.Load(an.ToString());
            foreach(Type type in asm.GetTypes()){   
                //PROPERTIES
                foreach (System.Reflection.PropertyInfo property in type.GetProperties()){
                    if (property.CanRead){
                        Response.Write("<br>" + an.ToString() + "." + type.ToString() + "." + property.Name);       
                    }
                }
                //METHODS
                var methods = type.GetMethods();
                foreach (System.Reflection.MethodInfo method in methods){               
                    Response.Write("<br><b>" + an.ToString() + "."  + type.ToString() + "." + method.Name  + "</b>");   
                    foreach (System.Reflection.ParameterInfo param in method.GetParameters())
                    {
                        Response.Write("<br><i>Param=" + param.Name.ToString());
                        Response.Write("<br>  Type=" + param.ParameterType.ToString());
                        Response.Write("<br>  Position=" + param.Position.ToString());
                        Response.Write("<br>  Optional=" + param.IsOptional.ToString() + "</i>");
                    }
                }
            }
        }

【讨论】:

  • 顺便说一句...我从最初的帖子中排除了它,但我过滤了一些回复,比如foreach(Type type in asm.GetTypes()){ if ((type.ToString().IndexOf("ACLASSIMLOOKINGFOR")&gt;=0) || (type.ToString().IndexOf("BCLASSIMLOOKINGFOR")&gt;=0)){...
  • 这根本不能回答问题。
  • 我没有使用 GetExecutingAssembly(),而是使用了 GetEntryAssembly() 来确保获得我的程序使用的更好的程序集列表。如果 ExecutingAssembly 恰好是一个 DLL,我会错过其中的一些。
  • 尝试使用:Assembly[] assembly = AppDomain.CurrentDomain.GetAssemblies();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多