如何获取dll中仅真正使用的方法 [英] How get only really used methods in an dll

查看:86
本文介绍了如何获取dll中仅真正使用的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在尝试开发一个应用程序,该应用程序读取另一个dll的所有引用的dll.通过这些参考,我得到了所有使用的方法.但是我不想得到所有现有的方法.我只想获取真正使用的方法,即仅读取由dll调用的方法.有什么办法可以使用反射来做到这一点?

抱歉,我的英语不好.

谢谢,

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

推荐答案

实际上,我正在尝试列出dll的所有依赖项.在导入表中,我具有所有对dll的引用的信息.原来,这些
引用没有明确显示调用了哪些方法.除了知道什么是dll引用外,还想知道dll引用使用了哪些方法.

这是我正在使用的代码:

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);
  }
}



我希望我能更好地解释它.Thaks.



I hope I have explained it better.Thaks.


我可能没有正确理解您的问题,但我看不到您要做什么的重点.

加载dll时,将加载所有dll,而不仅仅是使用的成员.

还是我误会了?
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?


感谢您提出有趣的问题!

正如我所说,如果您拥有源代码,这很容易(Visual Studio可以自动执行).如果没有,我只知道一种方法:反汇编所有有问题的引用程序集并分析其结果.您始终可以以编程方式使用反汇编程序,您所需要的只是重新分布的.NET Framework.

(您不应使用术语"DLL".与本机Windows相比,所有.NET程序集在本质上都是相同的.您可以以与*.DLL完全相同的方式引用*.exe,您知道吗?)

您应该了解需要分析引用程序集.在引用的程序集中使用的结果取决于哪个程序集使用它.您还应该了解静态已知永远不会调用的方法之间的区别,因为在引用程序集中没有调用代码.有一些原则上可以调用的方法(存在调用代码),但是运行代码的动态行为不会调用这些方法.很容易证明,整理出这些方法在理论上是无法解决的问题.可以从众所周知的halting problem推断出答案: http://en.wikipedia.org/wiki/Halting_problem [< ^ ],另请参见http://en.wikipedia.org/wiki/Computability_theory [ ^ ].

因此,让我们集中讨论第一个更简单的问题(静态分辨率).

附带说明一下,您应该了解该方法仅按需进行JIT编译.这意味着即使您捕获了JIT编译,也无法帮助您解决问题.

不幸的是,据我所知,无法使用反射解决此问题.解决程序集依赖关系也很容易,您可能知道这一点.就是说,我不是100%肯定没有解决方案.如果您遇到解决问题的方法,请分享.

谢谢.

—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


这篇关于如何获取dll中仅真正使用的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆