VS2015中的混合模式汇编MSTest失败 [英] Mixed-Mode Assembly MSTest Failing in VS2015

查看:196
本文介绍了VS2015中的混合模式汇编MSTest失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS2015中尝试运行使用混合模式程序集的单元测试时,测试无法执行,并显示通常的消息:

When attempting to run unit tests that use mixed mode assemblies in VS2015 the tests fail to execute with the usual message:

System.IO.FileLoadException:混合模式程序集是针对运行时的版本"v2.0.50727"构建的,没有附加配置信息就无法在4.0运行时中加载.

System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

创建一个app.config并向其中添加useLegacyV2RuntimeActivationPolicy无效-似乎无法更改此配置.

Creating an app.config and adding useLegacyV2RuntimeActivationPolicy to it has no effect - it seems as though this configuration is impossible to change.

此功能以前在VS2013中无需手动操作即可.

推荐答案

替代1:配置

将启动配置添加到C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\TE.ProcessHost.Managed.exe.config:

<startup useLegacyV2RuntimeActivationPolicy="true">
</startup>

替代2:在运行时

这可能会停止工作.

只需将此类添加到单元测试项目中():

Simply add this class to the unit test project (source):

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public static class RuntimePolicyHelper
{
    [AssemblyInitialize]
    public static void SetPolicy(TestContext ctx)
    {
        var clrRuntimeInfo =
            (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
                Guid.Empty,
                typeof(ICLRRuntimeInfo).GUID);

        // Allow errors to propagate so as to fail the tests.
        clrRuntimeInfo.BindAsLegacyV2Runtime();
    }

    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
    private interface ICLRRuntimeInfo
    {
        void xGetVersionString();
        void xGetRuntimeDirectory();
        void xIsLoaded();
        void xIsLoadable();
        void xLoadErrorString();
        void xLoadLibrary();
        void xGetProcAddress();
        void xGetInterface();
        void xSetDefaultStartupFlags();
        void xGetDefaultStartupFlags();

        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
        void BindAsLegacyV2Runtime();
    }
}

这篇关于VS2015中的混合模式汇编MSTest失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆