簡體   English   中英

如何在c#中動態加載已簽名的程序集

[英]How to load a signed assembly dynamically in c#

Assembly myDll = Assembly.LoadFrom("D:\\ClassLib.dll, Version=1.0.0.0, 
                                   Culture=neutral, PublicKeyToken=173d654960d26d29");

我得到例外說“無法加載程序集”請在這里幫助我。

有關加載程序集,請參閱MSDN文檔。 嘗試僅將匯編路徑作為此方法的參數,它將按照文檔中的示例工作。 為了檢查裝配的真實性,您可以執行以下操作:

    string filePath = "C:/path/to/file.dll";
    Assembly assembly = Assembly.LoadFrom(filePath);
    Module module = assembly.GetModules().First();
    X509Certificate certificate = module.GetSignerCertificate();
    if (certificate == null)
    {
         // file is not signed.
    }

事實上,一旦加載了程序集,就會在程序集對象中擁有大多數屬性,例如版本號等。

您可以按如下方式加載程序集

string path = @"D:\ClassLib.dll";
Assembly assembly = Assembly.LoadFrom(path);

更多來源在這里

根據MSDN網站上的這篇文章 ,有一種更好的方法來檢查有效性。

  1. 將這兩行代碼粘貼到相關類的頂部。
[DllImport("mscoree.dll", CharSet = CharSet.Unicode)]
static extern bool StrongNameSignatureVerificationEx(string wszFilePath, bool fForceVerification, ref bool pfWasVerified);
  1. 加載程序集
var filePath = "C:/whatever.dll";
var assembly = Assembly.LoadFrom(filePath);
  1. 根據您的需要設置最后2個參數並調用該功能。 小幫手表
var alertMessage = "Spoofed!";
var isValid = StrongNameSignatureVerificationEx(filePath, ....);
if (!isValid)
    throw new FileNotFoundException(alertMessage, filePath);

注意:據我所知,沒有托管版本。 因此,使用此功能時是線程安全的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM