2010-07-18 75 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的信息。