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

时间:2015-03-30 05:36:47

标签: c# .net xunit coderush

我的一个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,如果我没有指定完全相同的版本,我就不会再出现异常。

我最初认为这是由.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 突然忽略程序集限定类型名称的 version 部分?

2 个答案:

答案 0 :(得分:3)

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

如果你的跑步者正在使用它,这可能就是为什么你只在单元测试中遇到这个问题。

答案 1 :(得分:0)

该问题与xUnit或我的项目引用的任何其他程序集无关。我已经能够在minimalist project中复制相同的行为,并且这种奇怪行为的来源似乎源于我用来与我的测试进行交互的CodeRush(14.2.6.0)工具。

opened an issue with DevExpress我会在收到支持小组的回复后更新此答案。