Ok, I will preface this with a little information. In my current application I have a reports dll that contains all the reports for the application. Now in order to return a report based on it's name I originally had to use some very long methods in order to determine the appropriate report object to return. In an effort to create another method I stumbled upon this and figured I would share. Not only is this much less code then I used too use, it has also greatly improved performance in loading reports. All of my reports now load at least twice as fast as they used to. Anyway here is my report class that is used to return report objects by name.
VB.NET Code:
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Reflection
Public Class Report
''' <summary>
''' Get a report from this assembly
''' </summary>
''' <param name="ReportName">The name of the report</param>
''' <returns>Report Document</returns>
''' <remarks></remarks>
Public Shared Function GetReport(ByVal ReportName As String) As ReportDocument
If ReportName.EndsWith(".rpt") Then
'Remove the .rpt if necessary
ReportName = ReportName.Replace(".rpt", String.Empty)
End If
'Get the calling assembly
Dim oAssembly As Assembly = Assembly.GetExecutingAssembly
'Ge the Reportdocument type
Dim t As Type = oAssembly.GetType(oAssembly.GetName.Name.ToString & "." & ReportName)
'Get the constructor info for the report
Dim conInfo As ConstructorInfo = t.GetConstructor(Type.EmptyTypes)
'Invoke the constructor and return the report document
Return CType(conInfo.Invoke(Nothing), ReportDocument)
End Function
End Class
I edited the example to simplify it a little more.