// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace AutomatedPerfTesting
{
///
/// Internal Utility Class
///
internal static class Util
{
private static Assembly[] AssemblyCache = null;
private static Assembly[] GetAssemblies()
{
if (AssemblyCache == null)
{
AssemblyCache = AppDomain.CurrentDomain.GetAssemblies().Reverse().ToArray();
}
return AssemblyCache;
}
///
/// Get array of types which match the given predicate.
///
/// Conditional predicate
/// Array of types
public static Type[] GetTypes(Func Predicate)
{
// Even though we cache the assemblies here, this function
// is still iterating through all types available within the
// assemblies to find matches.
List FoundTypes = new List();
foreach (Assembly CandidateAssembly in GetAssemblies())
{
Type[] CandidateTypes = CandidateAssembly.GetTypes();
List Types = CandidateTypes
.Where(Predicate)
.ToList();
FoundTypes.AddRange(Types);
}
return FoundTypes.ToArray();
}
///
/// Get reflected Type info of given type name if available.
///
/// Interface the type is derived from
/// Name of type
/// Returns instance of Type if available, otherwise returns null
public static Type GetTypeWithInterface(string Name)
{
return GetTypeByName(Name, typeof(TInterface));
}
///
/// Get reflected Type info of given type name if available. Searches assemblies
/// in current app domain.
///
/// Name of type
/// Interface the type is derived from. Optional
/// Returns instance of Type if available, otherwise returns null
public static Type GetTypeByName(string Name, Type InterfaceType = null)
{
if (InterfaceType == null)
{
InterfaceType = typeof(object);
}
foreach (Assembly CandidateAssembly in GetAssemblies())
{
Type CandidateType = CandidateAssembly.GetType(Name.Trim());
if (CandidateType != null && InterfaceType.IsAssignableFrom(CandidateType))
{
return CandidateType;
}
}
return null;
}
///
/// Get array of types which is assignable from given interface
/// type.
///
/// Interface the types should implement
///
public static Type[] GetTypesWithInterface()
{
Type InterfaceType = typeof(TInterface);
return GetTypes(Type => Type.IsClass && InterfaceType.IsAssignableFrom(Type));
}
public static Type[] GetTypesWithAttribute()
{
return GetTypesWithAttribute(typeof(TAttribute));
}
public static Type[] GetTypesWithAttribute(Type AttributeType)
{
return GetTypes(Type => Type.IsClass && Attribute.IsDefined(Type, AttributeType));
}
}
}