namespaces - 在引用 NSubstitute 的程序集中使用 DynamicProxy 类型时如何修复 "type exists in both assemblies"故障?

标签 namespaces assembly-resolution dynamic-proxy nsubstitute

我有一个使用 DynamicProxy 的应用程序3.1 做运行时拦截。我有一个使用 NSubstitute 的测试程序集 mock 。我刚刚针对完全自举的容器(使用 InterceptWith 进行拦截的 StructureMap)编写了一些“集成”测试,以便我可以断言从容器中出来的某些类型已被正确代理。

[Subject(typeof(RobotBoard))]
public class When_resolving_an_intercepted_type : WithContainer<IRobotBoard>
{
    It should_have_recovery = () => Subject.ShouldHaveInterceptor<RecoveryInterceptor>();
}

public static class TestExtensions
{
    public static void ShouldHaveInterceptor<T>(this object obj)
        where T : IInterceptor
    {
        ((IProxyTargetAccessor)obj)
            .GetInterceptors()
            .ToList()
            .Exists(x => x is T)
            .ShouldBeTrue();
    }
}

但是,我收到此错误,表明 DynamicProxy 引用也在 NSubstitute 程序集中内部! (它似乎被 ilmerged)。

Error    11    MyCompany.MyModule.Specifications    D:\code\source\tests\When_resolving_an_intercepted_type.cs
The type 'Castle.DynamicProxy.IProxyTargetAccessor' exists in both 'd:\code\packages\Castle.Core.3.1.0\lib\net40-client\Castle.Core.dll' and 'd:\code\packages\NSubstitute.1.4.2.0\lib\NET40\NSubstitute.dll'

这个冲突到底有没有?

最佳答案

您可以获取 NSubstitute source code并从项目的目标中删除 ilmerge 命令。我打开了issue 86在他们的存储库中解决这个问题。

<exec command="&quot;$(MSBuildProjectDirectory)\..\..\ThirdParty\Ilmerge\ILMerge.exe&quot; /internalize:&quot;$(MSBuildProjectDirectory)\ilmerge.exclude&quot; /keyfile:$(AssemblyOriginatorKeyFile) /out:@(MainAssembly)  &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')" Condition=" '$(TargetFrameworkVersion)' == 'v3.5'" />
<exec command="&quot;$(MSBuildProjectDirectory)\..\..\ThirdParty\Ilmerge\ILMerge.exe&quot; /internalize:&quot;$(MSBuildProjectDirectory)\ilmerge.exclude&quot; /keyfile:$(AssemblyOriginatorKeyFile) /out:@(MainAssembly) /targetplatform:&quot;v4,$(FrameworkReferenceAssemblyPath).&quot; &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')" Condition=" '$(TargetFrameworkVersion)' == 'v4.0'" />

关于namespaces - 在引用 NSubstitute 的程序集中使用 DynamicProxy 类型时如何修复 "type exists in both assemblies"故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12266279/

相关文章:

c++ - 如何在 C++ 中限制对静态变量的访问?

.net - 如何强制从哪里加载程序集?

c# - 有没有办法创建支持接口(interface)的 DynamicObject?

java - 如何创建动态Wrapper?

c# - 解析程序集异常 : Attempt to load an unverifiable executable with fixups

c# - 如何将 DynamicProxy "really"向下转换回其原始类型(通过 WCF 发送)

r - 在编写自己的 R 包时,我似乎无法正确导入其他包

c# - .NET (C#) 将消息从自定义控件传递到主应用程序

php spl_autoload 不适用于语法错误的命名空间

.net - fuslogvw中Default/NativeImage是什么意思?