Click here to Skip to main content
15,883,883 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 48.9K   572   11  
This article explains how to write unit tests for MVVM using Catel.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AppDomainExtensions.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   <see cref="AppDomain" /> extensions.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Reflection;

#if SILVERLIGHT
using System.Windows;
using System.Windows.Resources;
#endif

namespace Catel.Reflection
{
	/// <summary>
	/// <see cref="AppDomain"/> extensions.
	/// </summary>
	public static class AppDomainExtensions
	{
		/// <summary>
		/// Gets a list of all types inside the <see cref="AppDomain"/> that have the <paramref name="baseType"/> as base class.
        /// <para />
		/// If the specified base type is an interface, the types that are returned are types that implement the interface.
		/// If the specified base type is a class, the types that have the type as base class are returned.
		/// </summary>
		/// <param name="appDomain">The app domain.</param>
		/// <param name="baseType">Type of the base class.</param>
		/// <returns>List of types found in the <see cref="AppDomain"/>.</returns>
		public static Type[] GetTypes(this AppDomain appDomain, Type baseType)
		{
			if (appDomain == null)
			{
				throw new ArgumentNullException("appDomain");
			}

			if (baseType == null)
			{
				throw new ArgumentNullException("baseType");
			}

			List<Type> types = new List<Type>();

			List<Assembly> assemblies = AssemblyHelper.GetLoadedAssemblies(appDomain);
			foreach (Assembly assembly in assemblies)
			{
				Type[] assemblyTypes = assembly.GetTypes();
				foreach (Type assemblyType in assemblyTypes)
				{
					if (baseType.IsInterface)
					{
						if (assemblyType.GetInterface(baseType.Name, false) != null)
						{
							types.Add(assemblyType);
						}
					}
					else if (baseType.IsClass)
					{
						if (assemblyType.IsSubclassOf(baseType))
						{
							types.Add(assemblyType);
						}
					}
				}
			}

			return types.ToArray();
		}

		/// <summary>
		/// Gets a list of all types inside the <see cref="AppDomain"/> that have the <typeparamref name="TBaseType"/> as base class.
		/// </summary>
		/// <typeparam name="TBaseType">The type of the base class.</typeparam>
		/// <param name="appDomain">The app domain.</param>
		/// <returns>
		/// List of types found in the <see cref="AppDomain"/>.
		/// </returns>
		public static Type[] GetTypes<TBaseType>(this AppDomain appDomain)
		{
			return GetTypes(appDomain, typeof(TBaseType));
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions