簡體   English   中英

如何從另一個(測試).Net dll 訪問 .Net DLL 中的嵌入資源?

[英]How can I access an embedded resource in a .Net DLL from another (test) .Net dll?

我正在嘗試編寫一個訪問與項目關聯的資源的單元測試。 這是一般結構。 該資源具有供 MyLibClass 使用的字符串,並且該類設置為en-US

Solution1
--MyLibrary
  --Properties
     --AssemblyInfo.cs
     --Resources.resx
     --Resource.Designer.cs
  --References
  --MyLibClass.cs

--MyLibrary.Tests
  --Properties
     --AssemblyInfo.cs
  --References
  --MyLibClassTests.cs

--MainProject

測試需要設置類並將其傳遞給 ResourceManager,因為我正在嘗試使用依賴注入。 當我嘗試在測試中使用下面的代碼加載時,出現以下錯誤。

既然資源是嵌入在 MyLibrary.dll 中的,那么 MyLibraryTest.dll 怎么訪問呢?

  resmgr = new ResourceManager("MyLibrary",
                        Assembly.GetExecutingAssembly());

NUnit 中的錯誤

MyLibrary.Tests. MyLibClassTests.IsInValidMyProperty_Blank:
An unexpected exception type was thrown
Expected: System.ArgumentException
 but was: System.Resources.MissingManifestResourceException : Could not find any
resources appropriate for the specified culture or the neutral culture.  Make sure
"MyLibrary.resources" was correctly embedded or linked into assembly "MyLibrary.Tests"
at compile time, or that all the satellite assemblies required are loadable and
fully signed.

因此,在考慮之后,dll 位於單獨的項目文件夾中。

有沒有更好的方法來解決這個問題?

在您的示例中,您已經通過程序集從中加載資源 - 您只是出於某種原因使用不包含您正在尋找的資源的程序集。 很可能您需要傳遞MyLibClass所在的相同程序集,而不是我假設為您提供測試代碼程序集的Assembly.GetExecutingAssembly()

resmgr = new ResourceManager("MyLibrary",
    typeof(MyLibClass).Assembly);

暫無
暫無

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

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