gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:28 25 4
gpt4 key购买 nike

我正在尝试编写一个单元测试来访问我与项目相关联的资源。这是一般结构。该资源包含旨在供 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com