Catel.Reflection.TypeCache.InitializeTypes C# (CSharp) Method

InitializeTypes() public static method

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. The types initialized by this method are used by object.GetType.
public static InitializeTypes ( Assembly assembly = null, bool forceFullInitialization = false ) : void
assembly System.Reflection.Assembly The assembly to initialize the types from. If null, all assemblies will be checked.
forceFullInitialization bool If true, the types are initialized, even when the types are already initialized.
return void
        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);
            }
        }

Same methods

TypeCache::InitializeTypes ( bool forceFullInitialization, Assembly assembly = null ) : void
TypeCache::InitializeTypes ( bool forceFullInitialization, string assemblyName ) : void

Usage Example

Example #1
0
        /// <summary>
        /// Loads the assembly into the specified <see cref="AppDomain" />.
        /// </summary>
        /// <param name="appDomain">The app domain.</param>
        /// <param name="assemblyName">The assembly name.</param>
        /// <param name="includeReferencedAssemblies">if set to <c>true</c>, referenced assemblies will be included as well.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="appDomain"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="assemblyName"/> is <c>null</c>.</exception>
        public static void LoadAssemblyIntoAppDomain(this AppDomain appDomain, AssemblyName assemblyName, bool includeReferencedAssemblies = true)
        {
            Argument.IsNotNull("appDomain", appDomain);
            Argument.IsNotNull("assemblyName", assemblyName);

            Log.Debug("Preloading assembly '{0}'", assemblyName);

            try
            {
                if (appDomain.GetAssemblies().Any(assembly => AssemblyName.ReferenceMatchesDefinition(assemblyName, assembly.GetName())))
                {
                    Log.Debug("Assembly already loaded into the AppDomain");
                    return;
                }

                var loadedAssembly = appDomain.Load(assemblyName);
                if (includeReferencedAssemblies)
                {
                    Log.Debug("Loading referenced assemblies of assembly '{0}'", assemblyName);

                    var referencedAssemblies = loadedAssembly.GetReferencedAssemblies();
                    foreach (var referencedAssembly in referencedAssemblies)
                    {
                        LoadAssemblyIntoAppDomain(appDomain, referencedAssembly, false);
                    }
                }

                // Convenience call
                TypeCache.InitializeTypes(false, loadedAssembly);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to load assembly '{0}'", assemblyName);
            }
        }
All Usage Examples Of Catel.Reflection.TypeCache::InitializeTypes