Ejemplo n.º 1
0
        /// <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());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes the types. 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="assemblyName">Name of the assembly. If <c>null</c>, all assemblies will be checked.</param>
        /// <param name="forceFullInitialization">If <c>true</c>, the types are initialized, even when the types are already initialized.</param>
        /// <param name="allowMultithreadedInitialization">If <c>true</c>, allow multithreaded initialization.</param>
        /// <exception cref="ArgumentException">The <paramref name="assemblyName"/> is <c>null</c> or whitespace.</exception>
        public static void InitializeTypes(string assemblyName, bool forceFullInitialization, bool allowMultithreadedInitialization = false)
        {
            // Important note: only allow explicit multithreaded initialization

            Argument.IsNotNullOrWhitespace("assemblyName", assemblyName);

            lock (_lockObject)
            {
                if (_loadedAssemblies.Any(x =>
                {
                    var assemblyNameWithoutVersion = TypeHelper.GetAssemblyNameWithoutOverhead(x);
                    if (string.Equals(assemblyNameWithoutVersion, assemblyName))
                    {
                        return(true);
                    }

                    return(false);
                }))
                {
                    // No need to initialize (or get the loaded assemblies)
                    return;
                }

                var loadedAssemblies = AssemblyHelper.GetLoadedAssemblies();

                foreach (var assembly in loadedAssemblies)
                {
                    try
                    {
                        if (assembly.FullName.Contains(assemblyName))
                        {
                            InitializeTypes(assembly, forceFullInitialization, allowMultithreadedInitialization);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Warning(ex, $"Failed to get all types in assembly '{assembly.FullName}'");
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes the types in Silverlight. 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="assemblyName">Name of the assembly. If <c>null</c>, all assemblies will be checked.</param>
        /// <exception cref="ArgumentException">The <paramref name="assemblyName"/> is <c>null</c> or whitespace.</exception>
        public static void InitializeTypes(bool forceFullInitialization, string assemblyName)
        {
            Argument.IsNotNullOrWhitespace("assemblyName", assemblyName);

            lock (_lockObject)
            {
                foreach (var assembly in AssemblyHelper.GetLoadedAssemblies())
                {
                    try
                    {
                        if (assembly.FullName.Contains(assemblyName))
                        {
                            InitializeTypes(forceFullInitialization, assembly);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Warning(ex, "Failed to get all types in assembly '{0}'", assembly.FullName);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <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();
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public static void InitializeTypes(Assembly assembly = null, bool forceFullInitialization = false, bool allowMultithreadedInitialization = false)
        {
            // Important note: only allow explicit multithreaded initialization

            var checkSingleAssemblyOnly = assembly != null;

            lock (_lockObject)
            {
                if (_hasInitializedOnce && !forceFullInitialization && !checkSingleAssemblyOnly)
                {
                    return;
                }

                if (!checkSingleAssemblyOnly)
                {
                    _hasInitializedOnce = true;
                }

                // CTL-877 Only clear when assembly != null
                if (forceFullInitialization && assembly is null)
                {
                    _loadedAssemblies.Clear();
                    _typesByAssembly?.Clear();
                    _typesWithAssembly?.Clear();
                    _typesWithAssemblyIgnoreCase?.Clear();
                    _typesWithoutAssembly?.Clear();
                    _typesWithoutAssemblyIgnoreCase?.Clear();
                }

                var assembliesToInitialize = checkSingleAssemblyOnly ? (IEnumerable <Assembly>) new [] { assembly } : AssemblyHelper.GetLoadedAssemblies();
                InitializeAssemblies(assembliesToInitialize, forceFullInitialization, allowMultithreadedInitialization);
            }
        }
Ejemplo n.º 6
0
        /// <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="assembly">The assembly to initialize the types from. If <c>null</c>, all assemblies will be checked.</param>
        /// <param name="forceFullInitialization">If <c>true</c>, the types are initialized, even when the types are already initialized.</param>
        public static void InitializeTypes(Assembly assembly = null, bool forceFullInitialization = false)
        {
            bool checkSingleAssemblyOnly = assembly != null;

            if (!forceFullInitialization && !checkSingleAssemblyOnly && (_typesWithAssembly != null))
            {
                return;
            }

            lock (_lockObject)
            {
                // CTL-877 Only clear when assembly != null
                if (forceFullInitialization && assembly == null)
                {
                    _loadedAssemblies.Clear();

                    if (_typesByAssembly != null)
                    {
                        _typesByAssembly.Clear();
                        _typesByAssembly = null;
                    }

                    if (_typesWithAssembly != null)
                    {
                        _typesWithAssembly.Clear();
                        _typesWithAssembly = null;
                    }

                    if (_typesWithAssemblyLowerCase != null)
                    {
                        _typesWithAssemblyLowerCase.Clear();
                        _typesWithAssemblyLowerCase = null;
                    }

                    if (_typesWithoutAssembly != null)
                    {
                        _typesWithoutAssembly.Clear();
                        _typesWithoutAssembly = null;
                    }

                    if (_typesWithoutAssemblyLowerCase != null)
                    {
                        _typesWithoutAssemblyLowerCase.Clear();
                        _typesWithoutAssemblyLowerCase = null;
                    }
                }

                if (_typesByAssembly == null)
                {
                    _typesByAssembly = new Dictionary <string, Dictionary <string, Type> >();
                }

                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 assembliesToInitialize = checkSingleAssemblyOnly ? new List <Assembly>(new[] { assembly }) : AssemblyHelper.GetLoadedAssemblies();
                InitializeAssemblies(assembliesToInitialize, forceFullInitialization);
            }
        }
Ejemplo n.º 7
0
        /// <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="assembly">The assembly to initialize the types from. If <c>null</c>, all assemblies will be checked.</param>
        /// <param name="forceFullInitialization">If <c>true</c>, the types are initialized, even when the types are already initialized.</param>
        public static void InitializeTypes(Assembly assembly = null, bool forceFullInitialization = false)
        {
            var checkSingleAssemblyOnly = assembly != null;

            if (!forceFullInitialization && !checkSingleAssemblyOnly && _typesWithAssembly.Count > 0)
            {
                return;
            }

            lock (_lockObject)
            {
                // CTL-877 Only clear when assembly != null
                if (forceFullInitialization && assembly == null)
                {
                    _loadedAssemblies.Clear();
                    _typesByAssembly?.Clear();
                    _typesWithAssembly?.Clear();
                    _typesWithAssemblyLowerCase?.Clear();
                    _typesWithoutAssembly?.Clear();
                    _typesWithoutAssemblyLowerCase?.Clear();
                }

                var assembliesToInitialize = checkSingleAssemblyOnly ? new List <Assembly>(new[] { assembly }) : AssemblyHelper.GetLoadedAssemblies();
                InitializeAssemblies(assembliesToInitialize, forceFullInitialization);
            }
        }