Ejemplo n.º 1
0
        /// <summary>
        /// Checks all existing assemblies for having <see cref="InitializeOnLoadAttribute"/> and runs static constructors for found types.
        /// If static constructor of type already executed, nothing happens.
        /// Also initializes all assemblies that will be loaded in future.
        /// </summary>
        /// <param name="appDomain"></param>
        public static Task InitializeTypesFromAttribute(this AppDomain appDomain)
        {
            return(Task.Factory.StartNew(() =>
            {
                //To prevent multiple event handlers, first we remove existing handler, only then we add handler to always have only 1 active handler.
                appDomain.DisableInitializerForNewlyLoadedAssemblies();
                appDomain.EnableInitializerForNewlyLoadedAssemblies();

                //Proceed all loaded assemblies.
                var assemblies = appDomain.GetAssemblies();
                foreach (var assembly in assemblies)
                {
                    assembly.InitializeTypesFromAttribute();
                }
            }, TaskCreationOptions.LongRunning));
        }