|
// --------------------------------------------------------------------------------------------------------------------
// <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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.