in

TFS Advantage

Furthering Continuous Integration Efforts on Microsoft Visual Studio Team System Team Foundation Server

Tim Bassett

How to find all Assemblies with MSTest test classes in a directory in a single LINQ statement

I needed a component that would examine a directory and find all test assemblies in a directory.  This sounded like a job for LINQ.

var allFiles = from fileName in Directory.GetFiles(directory.FullName, "*.dll")
let assembly = Assembly.LoadFile(fileName)
let types = assembly.GetExportedTypes()
from Type t in types
where t.GetCustomAttributes(typeof(TestClassAttribute), true).Length > 0
select assembly ;

The above LINQ query will:

  1. Get all DLL (assemblies) files.
  2. Use the *non-preferred* way to load each of the files as an assembly using Assembly.LoadFile()
  3. Reflect all the public types in the assembly using Assembly.GetExportedTypes()
  4. Retrieve an array of any TestClassAttributes from each type and check to see if there are any
  5. Put the results of into an IEnumerable<Assembly>

Using this above, in a single statement, I have a managable list of all assemblies in the directory that have a least on MSTest test class in them.

Published May 05 2008, 03:45 PM by re101184
Filed under: , , ,

Comments

No Comments
Hound Dog Enterprises, LLC
Powered by Community Server (Non-Commercial Edition), by Telligent Systems