/// <summary> /// Get all avaliable reports under <see cref="Reports"/> namespace. /// </summary> /// <returns>A list of avaliable reports.</returns> public static Report[] GetReports() { var types = Assembly.GetExecutingAssembly().GetTypes(). Where(t => (t.IsClass) && (!t.IsAbstract) && (string.Equals(t.Namespace, "SystemSnapshotWebServer.Reports", StringComparison.Ordinal)) && (t.IsSubclassOf(typeof(Report))) ).ToArray(); var reports = new Report[types.Length]; for (var i = 0; i < types.Length; i++) { reports[i] = Activator.CreateInstance(types[i]) as Report; } return reports; }
/// <summary> /// Generate all reports in a list. /// </summary> /// <param name="reports">List of reports to generate.</param> /// <param name="saveReport">True for save reports in the html file.</param> /// <param name="filename">Sets the file full or relative path, if folder is given default name will be used in it.</param> /// <param name="maxDegreeOfParallelism">Sets the maximum number of concurrent tasks enabled by this ParallelOptions instance.</param> /// <returns><see cref="HtmlTemplate"/> with the reports already written in the template.</returns> public static HtmlTemplate GenerateReports(Report[] reports, bool saveReport = true, string filename = null, int maxDegreeOfParallelism = -1) { var htmlTemplate = new HtmlTemplate() { Filename = filename }; //List<Report> asyncReports = new List<Report>(); /*if (useSingleThread) { foreach (var report in reports) { //if (report.CanAsync) //{ // asyncReports.Add(report); // continue; //} report.Generate(); htmlTemplate.WriteFromVar(report.GetTemplateVar(), report.Html); } } else {*/ #if DEBUG maxDegreeOfParallelism = 1; #endif var options = new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }; Parallel.ForEach(reports, options, report => { Debug.WriteLine(report.GetTemplateVar()); report.Generate(); lock (htmlTemplate) { htmlTemplate.WriteFromVar(report.GetTemplateVar(), report.Html); } }); /*foreach (var report in reports) { htmlTemplate.WriteFromVar(report.GetTemplateVar(), report.Html); }*/ if (/*!ReferenceEquals(htmlTemplate, null) && */saveReport) { htmlTemplate.WriteToFile(); } return htmlTemplate; }