2

我目前正在使用以下方法来检查测试程序集:

private bool IsTestAssembly(string path)
{
    var assembly = Assembly.LoadFrom(path);
    foreach (var type in assembly.GetTypes())
    {
        var a = type.GetCustomAttributes(true).Select(x => x.ToString());
        if (a.Contains("Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute"))
            return true;
    }
    return false;
}

但是我想在不将程序集加载到内存中的情况下进行检查,因为我需要能够在之后删除它以防验证失败。

我希望我可以简单地卸载程序集,但我很快发现,根据MSDN

如果不卸载包含它的所有应用程序域,就无法卸载单个程序集。

提前致谢!

4

2 回答 2

2

我按照TheGreatCO的建议制定了一个简短的解决方案,即将程序集加载到新的AppDomain

1) 用法:

// assemblies are unloaded on disposal
using (var analyser = new AssemblyAnalyser())
{
    var path = "my.unit.tests.dll";
    var b = analyser.IsTestAssembly(path);
    Assert.IsTrue(b);
}

2)实施:

public class AssemblyAnalyser : MarshalByRefObject, IDisposable
{
    public AssemblyAnalyser()
    {
        var evidence = new Evidence(AppDomain.CurrentDomain.Evidence);
        var appSetup = new AppDomainSetup()
        {
            ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
        };
        appDomain = AppDomain.CreateDomain(otherDomainFriendlyName, evidence, appSetup); 
    }

    public bool IsTestAssembly(string assemblyPath)
    {
        if (AppDomain.CurrentDomain.FriendlyName != otherDomainFriendlyName)
        {
            var analyser = appDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, GetType().FullName);
            return ((AssemblyAnalyser)analyser).IsTestAssembly(assemblyPath);
        }
        else
        {
            var assembly = Assembly.LoadFrom(assemblyPath);
            return ContainsTestClasses(assembly);
        }
    }

    public void Dispose()
    {
        if (AppDomain.CurrentDomain.FriendlyName != otherDomainFriendlyName)
        {
            AppDomain.Unload(appDomain);
            GC.SuppressFinalize(this);
        }
    }

    ~AssemblyAnalyser()
    {
        Dispose();
    }

    private bool ContainsTestClasses(Assembly assembly)
    {
        foreach (var type in assembly.GetTypes())
        {
            var attr = type.GetCustomAttributes(true).Select(x => x.ToString());
            if (attr.Contains("Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute"))
                return true;
        }
        return false;
    }

    private const string otherDomainFriendlyName = "AssemblyAnalyser";

    private AppDomain appDomain;
}
于 2013-04-22T17:26:40.437 回答
1

使用Mono.Cecil检查程序集。Cecil 不需要加载程序集来检查它。

于 2013-04-19T19:04:09.310 回答