c# - 如何从另一个(测试).Net DLL 访问 .Net DLL 中的嵌入资源?

标签 c# winforms nunit embedded-resource

我正在尝试编写一个单元测试来访问与项目关联的资源。这是一般结构。该资源具有供 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,因为我正在尝试使用依赖注入(inject)。当我尝试在测试中使用下面的代码加载时,出现以下错误。

由于资源嵌入在 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);

关于c# - 如何从另一个(测试).Net DLL 访问 .Net DLL 中的嵌入资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16867882/

相关文章:

c# - 何时在 C# 的接口(interface)方法中使用任务?

C# 在应用程序中截取 .net 控件的屏幕截图并附加到 Outlook 电子邮件

c# - 将时间跨度转换为日期时间添加到 C# 中的日期时间选择器

c# - NUnit 不会运行我的单元测试;说测试适配器丢失,但我确实安装了它

c# - 如何在 VSTS 上配置单元测试名称/描述?

c# - 在 XNA/C# 中添加自定义光标?

c# - SQL Server BCP 实用程序最好使用的分隔符

c# - 如何在C#中复制列表

c# - 移除 ToolStripMenuItem 左边框

.net - NUnit 测试在 OpenCover 中失败