I have many references in my application, How do I add a unit test to check at runtime if all my references can be loaded?

When you get to the stage of packaging your application, missing 1 dll can mean a disaster. It is very important to check that your application's references can actually be loaded. Add the following unit test to check for this at runtime:

using System;
using System.Collections;
using System.Reflection;
using NUnit.Framework;

namespace UnitTest
{
	/// <summary>
	/// Forces load of all assemblies to stop any problems when the Setup Package doesn't have all the correct DLLs 
	/// </summary>
	/// 
	[TestFixture()]
	public class DLLDependencyTest
	{
		private static ArrayList m_LoadedDLLs;

		public DLLDependencyTest()
		{
			m_LoadedDLLs = new ArrayList();
			TestLoadReferencedDLLs();
		}

		[Test()]
		public void TestLoadReferencedDLLs()
		{
			Assembly applicationToTest = AppDomain.CurrentDomain.Load("Your File's Name");
			LoadDLL(applicationToTest);
		}

		private static void LoadDLL(Assembly assembly)
		{
			AssemblyName[] referencedAssemblies = assembly.GetReferencedAssemblies();
			foreach (AssemblyName referencedAssemblyName in referencedAssemblies)
			{
				Assembly currentReferencedAssembly = Assembly.Load(referencedAssemblyName);
				if (!m_LoadedDLLs.Contains(currentReferencedAssembly.FullName))
				{
					m_LoadedDLLs.Add(currentReferencedAssembly.FullName);
					LoadDLL(currentReferencedAssembly);
				}
			}
		}
	}
}

Spot an error? Please comment about this article

Keywords:   

New Search SSW Custom Support