/// <summary> /// Gets a list of all types inside the <see cref="AppDomain"/>. /// </summary> /// <param name="appDomain">The app domain.</param> /// <returns>List of types found in the <see cref="AppDomain"/>.</returns> /// <remarks> /// This class must only be used by Catel. To make sure that an application performs, make sure to use /// <see cref="TypeCache.GetTypes()"/> instead. /// </remarks> internal static Type[] GetTypes(this AppDomain appDomain) { Argument.IsNotNull("appDomain", appDomain); List <Assembly> assemblies = AssemblyHelper.GetLoadedAssemblies(appDomain); var types = new List <Type>(); foreach (var assembly in assemblies) { types.AddRange(from assemblyType in AssemblyHelper.GetAllTypesSafely(assembly) select assemblyType); } return(types.ToArray()); }
/// <summary> /// Initializes the types in the specified assembly. It does this by looping through all loaded assemblies and /// registering the type by type name and assembly name. /// <para/> /// The types initialized by this method are used by <see cref="object.GetType"/>. /// </summary> /// <param name="forceFullInitialization">If <c>true</c>, the types are initialized, even when the types are already initialized.</param> /// <param name="assembly">The assembly to initialize the types from. If <c>null</c>, all assemblies will be checked.</param> public static void InitializeTypes(bool forceFullInitialization, Assembly assembly = null) { bool checkSingleAssemblyOnly = assembly != null; if (!forceFullInitialization && !checkSingleAssemblyOnly && (_typesWithAssembly != null)) { return; } lock (_lockObject) { if (forceFullInitialization) { _typesWithAssembly.Clear(); _typesWithAssembly = null; _typesWithAssemblyLowerCase.Clear(); _typesWithAssemblyLowerCase = null; _typesWithoutAssembly.Clear(); _typesWithoutAssembly = null; _typesWithoutAssemblyLowerCase.Clear(); _typesWithoutAssemblyLowerCase = null; } if (_typesWithAssembly == null) { _typesWithAssembly = new Dictionary <string, Type>(); } if (_typesWithAssemblyLowerCase == null) { _typesWithAssemblyLowerCase = new Dictionary <string, Type>(); } if (_typesWithoutAssembly == null) { _typesWithoutAssembly = new Dictionary <string, string>(); } if (_typesWithoutAssemblyLowerCase == null) { _typesWithoutAssemblyLowerCase = new Dictionary <string, string>(); } var typesToAdd = new HashSet <Type>(); var assembliesToLoad = new List <Assembly>(); if (assembly != null) { assembliesToLoad.Add(assembly); } else { assembliesToLoad.AddRange(AssemblyHelper.GetLoadedAssemblies()); } foreach (var loadedAssembly in assembliesToLoad) { try { foreach (var type in AssemblyHelper.GetAllTypesSafely(loadedAssembly)) { typesToAdd.Add(type); } } catch (Exception ex) { Log.Warning(ex, "Failed to get all types in assembly '{0}'", loadedAssembly.FullName); } } foreach (var type in typesToAdd) { if (ShouldIgnoreType(type)) { continue; } var newAssemblyName = TypeHelper.GetAssemblyNameWithoutOverhead(type.GetAssemblyFullNameEx()); string newFullType = TypeHelper.FormatType(newAssemblyName, type.FullName); if (!_typesWithAssembly.ContainsKey(newFullType)) { _typesWithAssembly[newFullType] = type; _typesWithAssemblyLowerCase[newFullType.ToLowerInvariant()] = type; var typeNameWithoutAssembly = TypeHelper.GetTypeName(newFullType); _typesWithoutAssembly[typeNameWithoutAssembly] = newFullType; _typesWithoutAssemblyLowerCase[typeNameWithoutAssembly.ToLowerInvariant()] = newFullType.ToLowerInvariant(); } } } }
private static void InitializeAssemblies(IEnumerable <Assembly> assemblies) { lock (_lockObject) { var typesToAdd = new Dictionary <Assembly, HashSet <Type> >(); foreach (var assembly in assemblies) { var loadedAssemblyFullName = assembly.FullName; try { if (_loadedAssemblies.Contains(loadedAssemblyFullName)) { continue; } _loadedAssemblies.Add(loadedAssemblyFullName); if (ShouldIgnoreAssembly(assembly)) { continue; } typesToAdd[assembly] = new HashSet <Type>(); foreach (var type in AssemblyHelper.GetAllTypesSafely(assembly)) { typesToAdd[assembly].Add(type); } } catch (Exception ex) { Log.Warning(ex, "Failed to get all types in assembly '{0}'", loadedAssemblyFullName); } } foreach (var assemblyWithTypes in typesToAdd) { foreach (var type in assemblyWithTypes.Value) { InitializeType(assemblyWithTypes.Key, type); } } #if NET var lateLoadedAssemblies = new List <Assembly>(); lock (_threadSafeAssemblyQueue) { while (_threadSafeAssemblyQueue.Count > 0) { var assembly = _threadSafeAssemblyQueue.Dequeue(); lateLoadedAssemblies.Add(assembly); } } if (lateLoadedAssemblies.Count > 0) { InitializeAssemblies(lateLoadedAssemblies); } #endif } }