Activator.CreateInstance导致nunit锁定,从而阻止了Visual Studio构建 [英] Activator.CreateInstance causing nunit to lock which prevents visual studio building

查看:146
本文介绍了Activator.CreateInstance导致nunit锁定,从而阻止了Visual Studio构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行可疑代码的测试后;在Nunit(或更确切地说是nunit-agent.exe)结束之前,我无法在Visual Studio中重建程序集。

After I run my test that runs my suspect code; I cannot rebuild the assembly in Visual Studio until Nunit (or more specifically nunit-agent.exe) is ended.

错误是:

Could not copy "C:\path\MyTests\MyTests.dll" to "bin\Debug\MyTests.dll". 
    Exceeded retry count of 10.
Unable to copy file "C:\path\MyTests\Debug\MyTests.dll" 
    to "bin\Debug\MyTests.dll". The process cannot access the file 
    'bin\Debug\MyTests.dll' because it is being used by another process.

当前解决方法是关闭nunit,重建然后重新打开nunit(然后进行测试)。 痛苦

Current workaround is to close nunit, rebuild and then reopen nunit (and then test). painful

红鲱鱼认为这是卷影复制问题或nunit项目中的项目基本路径设置。不是这些。就是这样的代码。

AppDomain dom = AppDomain.CreateDomain("some");
string fullPath = Assembly.GetExecutingAssembly().CodeBase
                  .Replace("file:///", "").Replace("/", "\\");
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = fullPath;
Assembly assembly = dom.Load(assemblyName);
Type type = assembly.GetType("ClassName");

IMyInterface obj = Activator.CreateInstance(type) as IMyInterface;

obj.ActionMessage(param1, param2);

我认为这是一个处置问题,因此我实现了IDisposable并将所需的代码添加到类班级名称。没用。

I thought this was a disposal problem, so I implemented IDisposable and added the required code to the class "ClassName". Did not work.

我该如何解决此问题?

推荐答案

解决方案是执行

AppDomain.Unload(dom);

完整方法:

public static object InstObject(Assembly ass, string className)
{
    AppDomain dom = null;
    try
    {
        Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);

        string fullPath = ass.CodeBase.Replace("file:///", "").Replace("/", "\\");
        AssemblyName assemblyName = new AssemblyName();
        assemblyName.CodeBase = fullPath;
        dom = AppDomain.CreateDomain("TestDomain", evidence, 
                 AppDomain.CurrentDomain.BaseDirectory, 
                 System.IO.Path.GetFullPath(fullPath), true);

        Assembly assembly = dom.Load(assemblyName);
        Type type = assembly.GetType(className);

        object obj = Activator.CreateInstance(type);

        return obj;
    }
    finally
    {
        if (dom != null) AppDomain.Unload(dom);
    }
}

这篇关于Activator.CreateInstance导致nunit锁定,从而阻止了Visual Studio构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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