【问题标题】:How to fix "type exists in both assemblies" failure when using DynamicProxy types in an assembly referencing NSubstitute?在引用 NSubstitute 的程序集中使用 DynamicProxy 类型时,如何解决“两个程序集中都存在类型”故障?
【发布时间】:2012-09-04 15:02:59
【问题描述】:

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

[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 引用也在 inside NSubstitute 程序集! (它似乎被合并了)。

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'

这个冲突有什么问题吗?

【问题讨论】:

    标签: namespaces assembly-resolution dynamic-proxy nsubstitute


    【解决方案1】:

    您可以获取 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'" />
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用别名来引用 NSubstitute 或 DynamicProxy 程序集。

      请参阅MSDN How to: Use the Global Namespace Alias (C# Programming Guide) 了解更多信息。

      【讨论】:

        【解决方案3】:

        您可以使用此处说明的“外部别名”指令: http://blogs.msdn.com/b/ansonh/archive/2006/09/27/774692.aspx

        基本上

        (1) 在 VS 中,转到 FooVersion1 的程序集参考,然后右键单击 > 属性。

        (2) 将“别名”值更改为“FooVersion1”

        (3) 在您的 .cs 文件中使用:

        extern alias FooVersion1;
        using foo = FooVersion1::FooVersion1;
        ...
        var something = foo.FooClass();
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-23
        • 1970-01-01
        • 1970-01-01
        • 2011-03-17
        • 1970-01-01
        • 1970-01-01
        • 2017-06-04
        相关资源
        最近更新 更多