public void CodeGenerationCompleted(RazorBuildProvider provider, CodeGenerationCompleteEventArgs e)
 {
 }
        public void CodeGenerationStarted(RazorBuildProvider provider)
        {
            var assembliesToAdd = new List<Assembly>();

            DependencyDescriptor moduleDependencyDescriptor = GetModuleDependencyDescriptor(provider.VirtualPath);

            IEnumerable<DependencyDescriptor> dependencyDescriptors = _dependenciesFolder.LoadDescriptors();
            List<DependencyDescriptor> filteredDependencyDescriptors;
            if (moduleDependencyDescriptor != null)
            {
                // Add module
                filteredDependencyDescriptors = new List<DependencyDescriptor> { moduleDependencyDescriptor };

                // Add module's references
                filteredDependencyDescriptors.AddRange(moduleDependencyDescriptor.References
                    .Select(reference => dependencyDescriptors
                        .FirstOrDefault(dependency => dependency.Name == reference.Name)
                        ?? new DependencyDescriptor
                        {
                            LoaderName = reference.LoaderName,
                            Name = reference.Name,
                            VirtualPath = reference.VirtualPath
                        }
                        ));
            }
            else
            {
                // Fall back for themes
                filteredDependencyDescriptors = dependencyDescriptors.ToList();
            }

            var entries = filteredDependencyDescriptors
                .SelectMany(descriptor => _loaders
                                              .Where(loader => descriptor.LoaderName == loader.Name)
                                              .Select(loader => new
                                              {
                                                  loader,
                                                  descriptor,
                                                  references = loader.GetCompilationReferences(descriptor),
                                                  dependencies = _extensionDependenciesManager.GetVirtualPathDependencies(descriptor.Name)
                                              }));

            // Add assemblies
            foreach (var entry in entries)
            {
                foreach (var reference in entry.references)
                {
                    if (!string.IsNullOrEmpty(reference.AssemblyName))
                    {
                        var assembly = _assemblyLoader.Load(reference.AssemblyName);
                        if (assembly != null)
                            assembliesToAdd.Add(assembly);
                    }
                    if (!string.IsNullOrEmpty(reference.BuildProviderTarget))
                    {
                        // Returned assembly may be null if the .csproj file doesn't containt any .cs file, for example
                        var assembly = _buildManager.GetCompiledAssembly(reference.BuildProviderTarget);
                        if (assembly != null)
                            assembliesToAdd.Add(assembly);
                    }
                }
            }

            foreach (var assembly in assembliesToAdd)
            {
                provider.AssemblyBuilder.AddAssemblyReference(assembly);
            }

            // Add virtual path dependencies (i.e. source files)
            //PERF: Ensure each virtual path is present only once in the list of dependencies
            var virtualDependencies = entries
                .SelectMany(e => e.dependencies)
                .Distinct(StringComparer.OrdinalIgnoreCase)
                .ToList();

            foreach (var virtualDependency in virtualDependencies)
            {
                provider.AddVirtualPathDependency(virtualDependency);
            }

            // Logging
            if (Logger.IsEnabled(LogLevel.Debug))
            {
                if (assembliesToAdd.Count == 0 && provider.VirtualPathDependencies == null)
                {
                    Logger.Debug("CodeGenerationStarted(\"{0}\") - no dependencies.", provider.VirtualPath);
                }
                else
                {
                    Logger.Debug("CodeGenerationStarted(\"{0}\") - Dependencies: ", provider.VirtualPath);
                    if (provider.VirtualPathDependencies != null)
                    {
                        foreach (var virtualPath in provider.VirtualPathDependencies)
                        {
                            Logger.Debug("  VirtualPath: \"{0}\"", virtualPath);
                        }
                    }
                    foreach (var assembly in assembliesToAdd)
                    {
                        Logger.Debug("  Reference: \"{0}\"", assembly);
                    }
                }
            }
        }
        public void CodeGenerationStarted(RazorBuildProvider provider)
        {
            var assembliesToAdd = new List<Assembly>();

            var moduleDependencyDescriptor = GetModuleDependencyDescriptor(provider.VirtualPath);

            var dependencyDescriptors = _dependenciesFolder.LoadDescriptors();
            List<DependencyDescriptor> filteredDependencyDescriptors;
            if (moduleDependencyDescriptor != null)
            {
                //添加模块
                filteredDependencyDescriptors = new List<DependencyDescriptor> { moduleDependencyDescriptor };

                //添加模块引用
                filteredDependencyDescriptors.AddRange(moduleDependencyDescriptor.References
                    .Select(reference => dependencyDescriptors
                        .FirstOrDefault(dependency => dependency.Name == reference.Name)
                                         ?? new DependencyDescriptor
                                         {
                                             LoaderName = reference.LoaderName,
                                             Name = reference.Name,
                                             VirtualPath = reference.VirtualPath
                                         }
                    ));
            }
            else
            {
                filteredDependencyDescriptors = dependencyDescriptors.ToList();
            }

            var entries = filteredDependencyDescriptors
                .SelectMany(descriptor => _loaders
                    .Where(loader => descriptor.LoaderName == loader.Name)
                    .Select(loader => new
                    {
                        loader,
                        descriptor,
                        references = loader.GetCompilationReferences(descriptor),
                        dependencies = _extensionDependenciesManager.GetVirtualPathDependencies(descriptor.Name)
                    })).ToArray();

            //添加程序集
            foreach (var entry in entries)
            {
                foreach (var reference in entry.references)
                {
                    if (!string.IsNullOrEmpty(reference.AssemblyName))
                    {
                        var assembly = _assemblyLoader.Load(reference.AssemblyName);
                        if (assembly != null)
                            assembliesToAdd.Add(assembly);
                    }
                    if (!string.IsNullOrEmpty(reference.BuildProviderTarget))
                    {
                        //返回总成可能是null,如果。csproj文件不containt任何cs文件,例如
                        var assembly = _buildManager.GetCompiledAssembly(reference.BuildProviderTarget);
                        if (assembly != null)
                            assembliesToAdd.Add(assembly);
                    }
                }
            }

            foreach (var assembly in assembliesToAdd)
            {
                provider.AssemblyBuilder.AddAssemblyReference(assembly);
            }

            //添加虚拟路径依赖(源文件)
            var virtualDependencies = entries
                .SelectMany(e => e.dependencies)
                .Distinct(StringComparer.OrdinalIgnoreCase)
                .ToList();

            foreach (var virtualDependency in virtualDependencies)
            {
                provider.AddVirtualPathDependency(virtualDependency);
            }

            //日志。
            if (!Logger.IsEnabled(LogLevel.Debug))
                return;
            Logger.Debug("CodeGenerationStarted(\"{0}\") - Dependencies: ", provider.VirtualPath);
            foreach (var virtualPath in provider.VirtualPathDependencies)
                Logger.Debug("  VirtualPath: \"{0}\"", virtualPath);
            foreach (var assembly in assembliesToAdd)
                Logger.Debug("  Reference: \"{0}\"", assembly);
        }