2010-07-18 76 views
2

我正在使用LoadFrom()加载dll,但由于某种原因,此加载函数不适用于所有dll,我想加载3000 dll以从每个dll获取版权属性。为什么reflection.assembly loadfile不加载所有dll?

我的代码:

class ReverseDLL 
{ 
    private Assembly assembly; 
    private AssemblyDescriptionAttribute desc; 
    private AssemblyTitleAttribute title; 
    private AssemblyCopyrightAttribute copyRight; 

    public string getCopyright(string path) 
    { 
     try 
     { 
      //assembly = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(path)); 
      assembly = System.Reflection.Assembly.LoadFrom(path);//"C:\\Windows\\winsxs\\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\\msvcm90d.dll");//path);// LoadFrom(path); 

       desc = (AssemblyDescriptionAttribute) 
       AssemblyDescriptionAttribute.GetCustomAttribute(
       assembly, typeof(AssemblyDescriptionAttribute)); 

       title = (AssemblyTitleAttribute) 
       AssemblyTitleAttribute.GetCustomAttribute(
       assembly, typeof(AssemblyTitleAttribute)); 

       copyRight = (AssemblyCopyrightAttribute)AssemblyCopyrightAttribute.GetCustomAttribute(assembly, typeof(AssemblyCopyrightAttribute)); 
     } 
     catch 
     { 
       this.copyRight = new AssemblyCopyrightAttribute(""); 
     } 

     if (this.copyRight == null) 
      this.copyRight = new AssemblyCopyrightAttribute(""); 

     return copyRight.Copyright; 
    } 
} 
+2

是否所有的dll管理dll? “ – rkellerm 2010-07-18 08:08:55

+6

”不起作用“并不是一份非常详细的诊断报告......可能是由于您只是捕获所有异常而没有记录发生了什么事情。 (捕捉*这样的一切*非常非常*很少是正确的事情。)失败时有什么例外? – 2010-07-18 08:14:54

+0

您可以在捕获异常时打印出堆栈跟踪。很可能你会看到一些dll为你的程序运行的东西而构建的(例如,试图在x86上加载x64 dll,试图加载本地dll而不是.NET程序集等) – nos 2010-07-18 11:57:59

回答

4

我不知道没有你提供更多的信息(如错误)反映问题,但你可以尝试访问该文件本身:

string copyright = FileVersionInfo.GetVersionInfo(path).LegalCopyright; 

这可以访问文件系统元数据(就像你会在资源管理器中看到的一样),并且可以为托管和非托管DLL工作; 它需要元数据存在(它不看看属性)。

编辑:快速检查表明(如预期)编译器确实检查此属性并正确填充文件元数据。

+0

非常感谢这个建议,它是真的帮了很多, 我可以从文件中获得数字签名吗? – kimo 2010-07-18 08:30:48

+0

@ kimo - 我真的不知道。 – 2010-07-18 08:45:39

0

你试过停止异常吗? Ctrl + Alt + E,在引发框架异常时停止。异常消息应该给你一些关于为什么无法加载DLL的信息。