Using Reflection to return a Crytstal ReportDocument-VBForums
Results 1 to 1 of 1

Thread: Using Reflection to return a Crytstal ReportDocument

  1. #1

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Using Reflection to return a Crytstal ReportDocument

    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:
    1. Imports CrystalDecisions.CrystalReports.Engine
    2. Imports System.Reflection
    3.  
    4. Public Class Report
    5.  
    6.     ''' <summary>
    7.     ''' Get a report from this assembly
    8.     ''' </summary>
    9.     ''' <param name="ReportName">The name of the report</param>
    10.     ''' <returns>Report Document</returns>
    11.     ''' <remarks></remarks>
    12.     Public Shared Function GetReport(ByVal ReportName As String) As ReportDocument
    13.         If ReportName.EndsWith(".rpt") Then
    14.             'Remove the .rpt if necessary
    15.             ReportName = ReportName.Replace(".rpt", String.Empty)
    16.         End If
    17.         'Get the calling assembly
    18.         Dim oAssembly As Assembly = Assembly.GetExecutingAssembly
    19.         'Ge the Reportdocument type
    20.         Dim t As Type = oAssembly.GetType(oAssembly.GetName.Name.ToString & "." & ReportName)
    21.         'Get the constructor info for the report
    22.         Dim conInfo As ConstructorInfo = t.GetConstructor(Type.EmptyTypes)
    23.         'Invoke the constructor and return the report document
    24.         Return CType(conInfo.Invoke(Nothing), ReportDocument)
    25.     End Function
    26. End Class

    I edited the example to simplify it a little more.
    Last edited by bmahler; Jan 4th, 2008 at 10:07 AM.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0 PHP Image Gallery v2.0
    VB 2005
    Find Computers on a network Simple License Encryption SQL Server Database Access dll Use Reflection to Return Crystal ReportDocument Silently Print PDF Generic Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost) MSDN Design Guidelines API Reference Inno Setup Compiler Inno Setup Preprocessor ISTool - Fairly easy to use GUI for creating inno setup projects Connection Strings NAnt -Automated Builds Cruise Control .NET - Frontend for automated builds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.