InitializeTypes() public static méthode

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.
Résultat void
Exemple #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);
            }
        }
Exemple #2
0
        /// <summary>
        /// Registers the assemblies from a xap file stream. The assemblies are added to a local
        /// cache which will be used by the <see cref="GetLoadedAssemblies()"/> method.
        /// </summary>
        /// <param name="xapStream">The xap stream.</param>
        /// <param name="registerInBackground">If <c>true</c>, the assembly will be loaded in the background.</param>
        /// <returns>List of assemblies in the xap files.</returns>
        /// <remarks>
        /// This method requires that the xap stream contains an <c>AppManifest.xaml</c>.
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="xapStream"/> is <c>null</c>.</exception>
        public static void RegisterAssembliesFromXap(Stream xapStream, bool registerInBackground = false)
        {
            Argument.IsNotNull("xapStream", xapStream);

            try
            {
                string appManifest = new StreamReader(Application.GetResourceStream(new StreamResourceInfo(xapStream, null),
                                                                                    new Uri("AppManifest.xaml", UriKind.Relative)).Stream).ReadToEnd();

                var deploy = XDocument.Parse(appManifest).Root;

                var parts = (from assemblyParts in deploy.Elements().Elements()
                             select assemblyParts).ToList();

                foreach (var xe in parts)
                {
                    string source     = xe.Attribute("Source").Value;
                    var    asmPart    = new AssemblyPart();
                    var    streamInfo = Application.GetResourceStream(new StreamResourceInfo(xapStream, "application/binary"), new Uri(source, UriKind.Relative));

                    var assembly = asmPart.Load(streamInfo.Stream);
                    if ((assembly != null) && !_externalAssemblies.Contains(assembly))
                    {
                        _externalAssemblies.Add(assembly);

                        var action = new Action(() =>
                        {
                            Log.Debug("Initializing types for assembly '{0}'", assembly.FullName);

                            TypeCache.InitializeTypes(false, assembly);

                            RegisterAssemblyWithVersionInfo(assembly);

                            Log.Debug("Initialized types for assembly '{0}'", assembly.FullName);
                        });

                        if (registerInBackground)
                        {
                            TaskHelper.RunAndWait(new [] { action });
                        }
                        else
                        {
                            action();
                        }
                    }
                }
            }
            catch (Exception)
            {
                // TODO: Add logging?
            }
        }
Exemple #3
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>
        /// <param name="alreadyLoadedAssemblies">The already loaded assemblies.</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>
        private static void LoadAssemblyIntoAppDomain(this AppDomain appDomain, AssemblyName assemblyName, bool includeReferencedAssemblies,
                                                      HashSet <string> alreadyLoadedAssemblies)
        {
            Argument.IsNotNull("appDomain", appDomain);
            Argument.IsNotNull("assemblyName", assemblyName);

            try
            {
                if (alreadyLoadedAssemblies.Contains(assemblyName.FullName))
                {
                    return;
                }

                if (appDomain.GetAssemblies().Any(assembly => AssemblyName.ReferenceMatchesDefinition(assemblyName, assembly.GetName())))
                {
                    return;
                }

                alreadyLoadedAssemblies.Add(assemblyName.FullName);

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

                var loadedAssembly = appDomain.Load(assemblyName);

                // Note: actually load a type so the assembly is loaded
                var type = loadedAssembly.GetTypesEx().FirstOrDefault(x => x.IsClassEx() && !x.IsInterfaceEx());
                Log.Debug("Loaded assembly, found '{0}' as first class type", type.GetSafeFullName(false));

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

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

                TypeCache.InitializeTypes(loadedAssembly);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to load assembly '{0}'", assemblyName);
            }
        }