我有一个与 System.BadImageFormatException: Could not load file or assembly 类似的错误. 但是引发错误的条件不同。
Test Name: Test_DisableTestUserAccount
Test FullName:
Test Source:
Test Outcome: Failed
Test Duration: 0:00:00Result Message:
Unable to get type #typename#. Error: System.IO.FileLoadException: Could not load file or assembly 'ComponentName.IntTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a433f9a8cee952' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)
File name: 'ComponentName.IntTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=433f9a8cee952' ---> System.Security.SecurityException: Strong name validation failed. (Exception from HRESULT: 0x8013141A)
The Zone of the assembly that failed was:
MyComputer
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at Microsoft.VisualStudio.TestPlatform.MSTestFramework.TypeCache.LoadType(String typeName, String assemblyName)
条件:
- 该解决方案具有 C# 控制台应用程序及其 UnitTest 项目
- InternalsVisibleTo 从控制台应用程序到 Unit 项目启用
- 所有项目均已签名
- sln 平台有两种配置:x64 和 x86(其他已删除)
当我运行将访问控制台应用程序内部元素的单元测试用例时,测试用例将失败并抛出上述异常。
最佳答案
问题的根本原因是我使用 x64 平台构建 sln 并使用 x86 测试设置运行测试用例。
只需使用正确的测试设置平台来运行测试用例:
- 构建平台应与运行测试用例的平台相同。
截图:
关于.net - 系统.BadImageFormatException : Could not load file or assembly or one of its dependencies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20675044/