gpt4 book ai didi

c# - 当版本不匹配时,System.Type.GetType 不会抛出

转载 作者:行者123 更新时间:2023-11-30 16:08:24 27 4
gpt4 key购买 nike

我的 xUnit 测试之一,我使用以下代码来定位匹配类型:

var type = System.Type.GetType (typeName, throwOnError: false);

typeName 看起来像:

Epsitec.Lydia.EventStore.TestBinaryEventStore+SimpleEvent, Tests.Lydia.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=361fc18aa5d4142d

如果类型不完全匹配(例如,因为完整的 Version 部分限定类型名称不同),我曾经在 .NET 4.5.x 的早期版本中得到一个 System.IO.FileLoadException。由于我收到了最新的 Windows Update,它巧合地包含 .NET 4.5.2,如果我没有指定完全相同的版本,我将不再遇到异常。 p>

我最初认为这是由 .NET 4.5.2 中 GetType 的新行为引起的,但事实似乎并非如此。我在 MSDN's documentation 中没有找到有关此更改的任何信息.

我尝试在 xUnit 测试之外的项目中复制此行为,但随后我得到了预期的行为(System.IO.FileLoadException 被抛出因为类型不匹配)。

我在这里错过了什么?关于我应该如何进行调查有任何想法吗?

附加信息

我使用 Fuslogvw 进一步调查。正如此跟踪所示,类型解析确实失败:

*** Assembly Binder Log Entry  (01.04.2015 @ 09:11:19) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 12.0\COMMON7\IDE\EXTENSIONS\O3TVLY23.2NC\PlugIns\CR_ExtUnitTestRunnerNet4.exe
--- A detailed error log follows.

=== Pre-bind state information ===
LOG: DisplayName = Tests.Lydia.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4681e18aa5df9116
(Fully-specified)
LOG: Appbase = file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 12.0/COMMON7/IDE/EXTENSIONS/O3TVLY23.2NC/PlugIns
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = C:\Users\Arnaud\AppData\Local\Temp\85454750-eabd-4a3b-a5f2-91f3b104eba4
LOG: AppName = 85454750-eabd-4a3b-a5f2-91f3b104eba4
Calling assembly : Tests.Lydia.Framework, Version=1.1.1514.0, Culture=neutral, PublicKeyToken=4681e18aa5df9116.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: S:\git\rnd\lydia\Tests.Lydia.Framework\bin\Release\Tests.Lydia.Framework.dll.config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Tests.Lydia.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4681e18aa5df9116
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 12.0/COMMON7/IDE/EXTENSIONS/O3TVLY23.2NC/PlugIns/Tests.Lydia.Framework.DLL.
LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 12.0/COMMON7/IDE/EXTENSIONS/O3TVLY23.2NC/PlugIns/Tests.Lydia.Framework/Tests.Lydia.Framework.DLL.
LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 12.0/COMMON7/IDE/EXTENSIONS/O3TVLY23.2NC/PlugIns/Tests.Lydia.Framework.EXE.
LOG: Attempting download of new URL file:///C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO 12.0/COMMON7/IDE/EXTENSIONS/O3TVLY23.2NC/PlugIns/Tests.Lydia.Framework/Tests.Lydia.Framework.EXE.
LOG: Attempting download of new URL file:///S:/git/rnd/lydia/Tests.Lydia.Framework/bin/Release/Tests.Lydia.Framework.DLL.
LOG: Assembly download was successful. Attempting setup of file: S:\git\rnd\lydia\Tests.Lydia.Framework\bin\Release\Tests.Lydia.Framework.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Tests.Lydia.Framework, Version=1.1.1514.0, Culture=neutral, PublicKeyToken=4681e18aa5df9116
WRN: Comparing the assembly name resulted in the mismatch: Minor Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
LOG: Attempting download of new URL file:///S:/git/rnd/lydia/Tests.Lydia.Framework/bin/Release/Tests.Lydia.Framework/Tests.Lydia.Framework.DLL.
LOG: Attempting download of new URL file:///S:/git/rnd/lydia/Tests.Lydia.Framework/bin/Release/Tests.Lydia.Framework.EXE.
LOG: Attempting download of new URL file:///S:/git/rnd/lydia/Tests.Lydia.Framework/bin/Release/Tests.Lydia.Framework/Tests.Lydia.Framework.EXE.
LOG: All probing URLs attempted and failed.

更多上下文

我正在从 xUnit 测试中运行代码,该测试也引用了多个其他库。尝试在原始解决方案之外复制它仍然没有出现问题。

引用的程序集如何调整 GetType 以使其能够突然忽略程序集限定类型名称的版本部分?

最佳答案

xUnit 有代码注册到 AppDomain.AssemblyResolve 事件并忽略版本和签名:

    Assembly LoadAssembly(AssemblyName assemblyName)
{
var path = Path.Combine(folder, assemblyName.Name);
return LoadAssembly(path + ".dll") ?? LoadAssembly(path + ".exe");
}

https://github.com/xunit/xunit/blob/master/src/common/AssemblyHelper.cs

如果您的运行者正在使用它,这可能就是您仅在单元​​测试中遇到此问题的原因。

关于c# - 当版本不匹配时,System.Type.GetType 不会抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29339060/

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